how is boost_scoped_ptr RAII ?
-
-
I can do the following:
boost::scoped_ptr class;
class->DoSomething(); // Has not been initialized yet
class.reset(new MyClass);
Doesn't it go against RAII? I keep reading that scoped_ptr is RAII but i can use object before it is initialized. Thanks
and what happens when you run that?
-
I can do the following:
boost::scoped_ptr class;
class->DoSomething(); // Has not been initialized yet
class.reset(new MyClass);
Doesn't it go against RAII? I keep reading that scoped_ptr is RAII but i can use object before it is initialized. Thanks
So, apart from the fact you'll never be able to call an object class (naughty, naughty) how does writing:
boost::scoped_ptr ptr;
leave you with an unitialised object? Or in other words what's not initialised in that statement? If you can work that out you'll see that scoped_ptr supports RAII (or as I prefer to call it these days scoped based resource management, SBRM) perfectly. Cheers, Ash
-
and what happens when you run that?
-
and what happens when you run that?
-
you declared a pointer, but never pointed it to an object. what's to explain?
-
you declared a pointer, but never pointed it to an object. what's to explain?
-
Is the purpose of RAII not to allow such a thing? Currently i am acquiring a resource but do not initialize it.
elelont2 wrote:
Currently i am acquiring a resource but do not initialize it.
but you're not acquiring any resource; you're just declaring an uninitialized pointer. the scoped_ptr c'tor allows you to defer allocation - but you do need to allocate it at some time, before you start using it. look at boost's examples[^]:
boost::scoped_ptr x(new Shoe);
they allocate (acquire) the object while declaring the pointer. also:
class MyClass {
boost::scoped_ptr ptr;
public:
MyClass() : ptr(new int) { *ptr = 0; }
int add_one() { return ++*ptr; }
};they declare the pointer, but defer allocation until the 'add_one' call.
-
So, apart from the fact you'll never be able to call an object class (naughty, naughty) how does writing:
boost::scoped_ptr ptr;
leave you with an unitialised object? Or in other words what's not initialised in that statement? If you can work that out you'll see that scoped_ptr supports RAII (or as I prefer to call it these days scoped based resource management, SBRM) perfectly. Cheers, Ash
-
So, apart from the fact you'll never be able to call an object class (naughty, naughty) how does writing:
boost::scoped_ptr ptr;
leave you with an unitialised object? Or in other words what's not initialised in that statement? If you can work that out you'll see that scoped_ptr supports RAII (or as I prefer to call it these days scoped based resource management, SBRM) perfectly. Cheers, Ash
ny_old_iron class will be uninitialized. How is it initialized? Also, what do you mean by "object class"? RAII says that the object is initialized during acquisition. scoped_ptr is supposed to support RAII but it allows to defer allocation, which means that the object pointed by the scoped_ptr is uninitialized. Therefore i would say that scoped_ptr is not RAII.