Re: Creation and destruction responsibilities
On Sep 4, 6:33 pm, Miguel Guedes <miguel.a.gue...@gmail.com> wrote:
I'd like some clarification on the correct way to destroy an object.
As per the standard, or as per some arbitrary design rules?
I've seen a couple of open source projects make use of object
factories to instantiate classes and smart pointers to destroy
them when the ref_count reaches zero.
That's an appropriate solution for a few special cases. Cases
that are, in practice, far more likely to occur in a library
than in application code, so it's possible that they may be
appropriate in the library you're looking at.
pInstance = Factory::createSomething();
smart_ptr<Something> ptr = pInstance
// out of scope: smart_ptr destroys pInstance
My question is, isn't a creator entity supposed to be responsible for the
destruction of the objects it creates? Isn't this one of the golden rules=
correct OO code design and what RAII is all about?
The most "OO" solution would only use delete this: an object,
once created, is resposible for its own destruction. This is
also the most frequent case in well designed applications. But
it's not an absolute case, and there are cases when other
solutions might be more appropriate.
The name RAII is really a misnomer, since it is usually used to
refer to the fact that resource liberation is in a destructor,
regardless of where the resource was acquired. (It is also used
to refer to anything that is required to maintain program
coherence, even when resources are not involved.)
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34