Say I've got a class where the sole data member is something like std::string
or std::vector
. Do I need to provide a Copy Constructor, Destructor and Assignment Operator?
Answer
In case your class contains only vector/string objects as its data members, you don't need to implement these. The C++ STL classes (like vector, string) have their own copy ctor, overloaded assignment operator and destructor.
But in case if your class allocates memory dynamically in the constructor then a naive shallow copy will lead to trouble. In that case you'll have to implement copy ctor, overloaded assignment operator and destructor.
No comments:
Post a Comment