December 2001
Intermediate to advanced
304 pages
6h 25m
English
auto_ptrDifficulty: 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?
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?
What are the advantages and drawbacks of the following approach?
// Example 30-2
//
class X2
{
// ...
private:
auto_ptr<Y> y_;
};
Read now
Unlock full access