When do we have to use copy constructors?
问题 I know that C++ compiler creates a copy constructor for a class. In which case do we have to write a user-defined copy constructor? Can you give some examples? 回答1: The copy constructor generated by the compiler does member-wise copying. Sometimes that is not sufficient. For example: class Class { public: Class( const char* str ); ~Class(); private: char* stored; }; Class::Class( const char* str ) { stored = new char[srtlen( str ) + 1 ]; strcpy( stored, str ); } Class::~Class() { delete[]