5

I have the next question: If I have a class which contains for instance

Class A {  
public:  
    A();  
    ~A();  
    ...  
protected:  
    //Data  
    std::string str;  
    std::vector<char> vctChar;  
    std::vector<std::string> vctString;  
}  

and I use the DEFAULT C.Ctor and assignement. Suppose I know declare the object and use it for another object as C.Ctor, will the new object will have it own copy of data (string and vectors ?) or it will point to the same one of the first object ?

4 Answers 4

6

I use the DEFAULT C.Ctor and assignement. Suppose I know declare the object and use it for another object as C.Ctor, will the new object will have it own copy of data (string and vectors

If your class doesn't has pointers, then the default copy-constructor and operator= generated by the compiler would make deep-copy (effectively) of your member variables. So yes, the new objects (created using either copy-constructor or operator=) will have their own copy of data (string, and vectors).

However, if your class A has a member of type say struct X which has pointer fields, then you need to define copy-constructor and operator= for this struct X so that the default copy-constructor and operator= for class A would produce desired result as you expect.

Sign up to request clarification or add additional context in comments.

Comments

1

It will have its own copies of the string and vector members (although internally, the strings may still refer to the same char array, as long as none of them are changed - but this is OK).

For nonprimitive members, the default copy constructor and assignment operator calls their respective copy constructor / assignment operator, which works just fine for standard library classes. The problem arises with pointers, where the default is to just copy their value, which results in two pointers referencing the same address.

2 Comments

I didn't understand the answear about the string, so it will have a full copy, that if I'll change it it won't effect the other object ?
@Roman, sorry for the confusion. You will have two distinct string objects indeed, which are safe to use, modify, and dispose of. The fact that they initially may share the character array containing the actual string representation (to optimize memory usage) is just an implementation detail, although it's good to know. As soon as either of them is modified, it will create its own copy of the char array and modify only that, so the other string(s) won't be affected by the change.
1

Each will have their own, separate copy of the data.

If you want copies to share the same data, read up on boost::shared_ptr<> (then use std::shared_ptr<> instead if your compiler ships with it).

Comments

1

Yes. The synthesised copy constructor invokes the copy constructor of all members, and std::string/std::vector have copy constructors that properly copy all the elements too.

This is not the case for raw arrays hidden by pointers, which would not perform the deep copy automatically.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.