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 ?
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.