Question from the C++ test

Write a C++ constructor that takes a char pointer and copies the string into a new char array.

Hard

Consider the following code:

class Test
{
    private:
        char *str;
    public:
        Test(char *s1)
        {
            ….
        }
        ~Test()
        {
            delete[] str;
        }
}

void main()
{
    Test s(“test string”);
}

What is the correct definition of constructor Test, in order to ensure that code runs successfully?

Author: W3D TeamStatus: Published(Update)Question passed 6 times
Edit
0
Community Evaluations
developer avatar
Maxime
21/04/2024
should use delete[] in destructor