Item 31. Smart Pointer Members, Part 2: Toward a ValuePtr

I l @ ve RuBoard

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?

  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 ValuePtr s is not allowed.

    2. 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.

    3. 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 .

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