Item 29. Using auto_ptr

I l @ ve RuBoard

Item 29. Using auto_ptr

Difficulty: 5

This Item illustrates a common pitfall with using auto_ ptr . What is the problem, and how can you solve it?

  1. Consider the following function, which illustrates a common mistake when using auto_ptr :

     template<typename T> void f( size_t n ) {   auto_ptr<T> p1( new T );   auto_ptr<T> p2( new T[n] );   // ... more processing ... } 

    What is wrong with this code? Explain.

  2. How would you fix the problem? Consider as many options as possible, including the Adapter pattern, alternatives to the problematic construct, and alternatives to auto_ptr .

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