Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Example {
- public:
- // ...
- private:
- boost::scoped_ptr<Owned> data;
- };
- class C
- {
- Owned * o1;
- Owned * o2;
- public:
- C() : o1(new Owned), o2(new Owned) {}
- ~C() { delete o1; delete o2;}
- };
- auto_ptr<int> a(new int), b;
- // oops, after this, a is reset. But a copy was desired!
- // it does the copy&reset-of-original, but it's not restricted to only temporary
- // auto_ptrs (so, not to ones that are returned from functions, for example).
- b = a;
- ~Example() { ptr.~scoped_ptr<Owned>(); }
- class MyClass
- {
- public:
- MyClass();
- MyClass(MyClass const& copy);
- MyClass& operator=(MyClass const& copy);
- ~MyClass();
- private
- Data* d1;
- Data* d2;
- };
- MyClass::MyClass()
- :d1(NULL),d2(NULL)
- {
- // This is the most trivial case I can think off
- // But even it looks ugly. Remember the destructor is NOT called
- // unless the constructor completes (without exceptions) but if an
- // exception is thrown then all fully constructed object will be
- // destroyed via there destructor. But pointers don't have destructors.
- try
- {
- d1 = new Data;
- d2 = new Data;
- }
- catch(...)
- {
- delete d1;
- delete d2;
- throw;
- }
- }
Add Comment
Please, Sign In to add comment