Chapter 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?

  1. 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:

    1. Copying and assigning ValuePtrs is not allowed.

    2. Copying and assigning ValuePtrs is allowed and has the semantics of creating a copy of the owned Y object using the Y copy constructor.

    3. Copying and assigning ValuePtrs 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. ...

Get More Exceptional C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.