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

I l @ ve RuBoard

Item 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_; }; 
I l @ ve RuBoard


More Exceptional C++
More Exceptional C++: 40 New Engineering Puzzles, Programming Problems, and Solutions
ISBN: 020170434X
EAN: 2147483647
Year: 2001
Pages: 118
Authors: Herb Sutter

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net