Mail Archives: djgpp/1998/07/18/05:31:37
Destination: Erik Max Francis <max AT alcyone DOT com>
From: Gruber Gerhard
Group: comp.os.msdos.djgpp
Date: Fri, 17 Jul 1998 17:43:05 -0700:
>Michael Tanczos wrote:
>
>> When are deconstructors called when using classes? At the
>> end of the program if it's declared statically.. or when
>> you delete the object if it's declared dynamically? Or do
>> you need to call the deconstructor directly..
>
>Automatic (local) objects are deconstructed when they go out of scope.
Are you sure? Waht about code like this?
main()
{
class c1;
do something
if(x)
{
class c2;
}
}
Usually local variables are allocated by the compiler at the function entry
regardless wether they are in subscope or not. Your above statement would mean
that c1 is constructed at the entry of main (that's true) and deconstructed at
the exit of main (also true). But c2 would be constructed afer entering the
condition and deconstructed when leaving that condition. I'm nbot sure if this
is true. I rather think that c2 is internally handled just like c1.
>You should not be attempting to explicitly call a destructor unless you
>are doing something very, very strange.
That's why I usually write a function that resets the object and call this
function when I need it and also from the destructor.
i.e.:
class x {
public:
x();
~x();
Destroyx();
};
x:~x()
{
Destroyx();
}
x:Destroyx()
{
reset object
}
--
Bye,
Gerhard
email: sparhawk AT eunet DOT at
g DOT gruber AT sis DOT co DOT at
Spelling corrections are appreciated.
- Raw text -