Exercise 13.6:
Overload operators are functions that have the name operator followed by the symbol for the operator being defined. Hence, the assignment operator is a function named operator=.
Just as it does for the copy constructor, the compiler generates a synthesized copy-assignment operator for a class if the class does not define its own.
Exercise 13.8
class Has_ptr
{
public:
// default constructor
Has_ptr(const std::string &s = std::string()) :
ps(new std::string(s)), i(0) { }
// copy-assignment constructor
Has_ptr(const Has_ptr &orig) :
ps(new std::string(*orig.ps)), i(orig.i) { }
// assignment operator
Has_ptr &operator=(const Has_ptr &rhs);
private:
std::string *ps;
int i;
};
Has_ptr &Has_ptr::operator=(const Has_ptr &rhs)
{
ps = rhs.ps;
i = rhs.i;
return *this;
}