Re: Order of destruct of local variables
aleskx@gmail.com (Alex) wrote (abridged):
Just like memory allocation consumes available resources and can
be exhausted if allocated but not released, so can a DB connection
or a socket pool. From that point of view, when a resource is not
needed any more it becomes garbage that needs to be collected
somehow. In C++, the most convenient way is to automatically do
it for any type of resource by wrapping it in a class and have
it collected automatically at destruction time.
Except that often when a resource is freed there needs to be some final
work done, eg flushing buffers, and this can fail, and destructors don't
have a good way to report failure. They can't return a function result to
their caller, and it's dangerous for them to use exceptions. Destructors
are a little bit too special.
And this is inherent in their being automatic. If they aren't explicitly
called then the caller isn't explicitly prepared to handle their errors.
The model of (eg) Smalltalk is more explicit, which is a burden for the
programmer but at least lets errors be handled in the usual way. I think
in C++ there is a tendency to sweep this problem under the carpet, and
people simply don't check, eg, the return code of fclose(), in
destructors. Out of sight is out of mind.
-- Dave Harris, Nottingham, UK.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]