struct myType {
vector<char*> ls;
};
Here ls is holding pointers to char. If a user-defined copy constructor for myType is not provided, will myType‘s default copy constructor do a deep copy of ls?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The default copy constructor will copy all members – i.e. call their respective copy constructors.1 So yes, a
std::vector(being nothing special as far as C++ is concerned) will be duly copied.However, the memory pointed to by the
char*elements inside the vector will of course not, since C++ doesn’t know and doesn’t care what the pointers point to.But the solution here isn’t to provide a custom copy constructor. It’s to use a data structure instead of raw pointers (
char*) which does. And this happens to bestd::string(orstd::vector<char>depending on the intent).1 Thus creating a transitive closure of the copy operation – this is how deep copying is generally implemented, but of course an implementor of the copy operation can always break out of it.