Chapter 30. Smart Pointer Members, Part 1: A Problem with auto_ptr

Difficulty: 5

Most C++ programmers know they have to take special care for classes with pointer members. But what about classes with auto_ptr members? And can we make life safer for ourselves and our users by devising a smart pointer class designed specifically for class membership?

  1. Consider the following class:

    // Example 30-1
    //
    class X1
    {
      // ...
    private:
      Y* y_;
    };
    

    If an X1 object owns its pointed-at Y object, why can't the author of X1 use the compiler-generated destructor, copy constructor, and copy assignment?

  2. What are the advantages and drawbacks of the following approach?

    // Example 30-2
    //
    class X2
    {
      // ...
    private:
      auto_ptr<Y> y_;
    };
    

Solution

Recap: Problems of Pointer Members ...

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.