August 2012
Intermediate to advanced
976 pages
30h 17m
English
To provide valuelike behavior, each object has to have its own copy of the resource that the class manages. That means each HasPtr object must have its own copy of the string to which ps points. To implement valuelike behavior HasPtr needs
• A copy constructor that copies the string, not just the pointer
• A destructor to free the string
• A copy-assignment operator to free the object’s existing string and copy the string from its right-hand operand
The valuelike version of HasPtr is
class HasPtr {public: HasPtr(const std::string &s = std::string()): ps(new std::string(s)), i(0) { } // each
Read now
Unlock full access