Boost logo

Boost Users :

From: Christian Larsen (contact_at_[hidden])
Date: 2008-08-17 14:41:42


Hi,

This is just make sure I'm not being too careful in this situation. So
please confirm whether this is correct.

I have a class with two scoped_ptr members, and I want to pass two
pointers to the constructor, which will then transfer the ownership of
both to the two member scoped_ptrs. I assume the following is not safe:

(I didn't try compiling; using namespace boost and std.)

     struct B {};

     class A
     {
     public:
         A(B* b1, B* b2)
             : b1_(b1), b2_(b2)
         {}
     private:
         scoped_ptr<B> b1_;
         scoped_ptr<B> b2_;
     };

     // Create an A this way:
     A a(new B, new B);

Is it correct that this could fail if one of the 'new B's throw, thus
leaking before the constructor ever started?

If that is so, is this then the correct way to achieve the same in a
safe manner?

     struct B {};

     class A
     {
     public:
         A(auto_ptr<B>& b1, auto_ptr<B>& b2)
             : b1_(b1.release()), b2_(b2.release())
         {}
     private:
         scoped_ptr<B> b1_;
         scoped_ptr<B> b2_;
     };

     // Create an A this way:
     A a(auto_ptr<B>(new B), auto_ptr<B>(new B));

Best regards,
Christian


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net