Question:
I am puzzled at the way in which the assignment operator is overloaded in C++. Would you please show an example as to what efficient code for overloading this operator would look like. Specifically, my confusion is about how this function would copy old resources into new resources before return *this.
Answer:
The copy assignment operator for a class must do the following.
- make sure that all members are copied.
- make sure it calls assignment on any base classes it inherits from
- be safe from assignment to self.
// class string inherits from some class called datatypeclass string : public datatype{public: string(char *str,int len) { data_ = new char [len+1]; strncpy(data_,str,len) data_[len] = ‘