Item 31. Smart Pointer Members , Part 2: Toward a ValuePtr
Difficulty: 6
Can we make life safer for ourselves and our users by devising a smart pointer class designed specifically for class membership?
Write a suitable ValuePtr template that is used as shown here:
// Example 31-1 // class X { // ... private: ValuePtr<Y> y_; };
to suit three specific circumstances:
Copying and assigning ValuePtr s is not allowed.
Copying and assigning ValuePtr s is allowed and has the semantics of creating a copy of the owned Y object using the Y copy constructor.
Copying and assigning ValuePtr s is allowed and has the semantics of creating a copy of the owned Y object, which is performed using a virtual Y::Clone() method if present and the Y copy constructor otherwise .