Hi,
Maybe it's written in every C++ book or C++ tutorial, but thanks to a bug, I have found a strange behaviour of C++ classes.
When defining in a class a virtual destructor, it will be automatically called when its heirs' destructor are called.
Example :
When executing this program, you get :
But in virtual features(excluding destructor), the base feature is not automatically called, you have to do it manually, and that's normal, because you need flexibility when you redefine a feature. But don't call the parent's destructor in the destructor of a class, because it will be called twice!
So be careful with that!!
--
Globos
Maybe it's written in every C++ book or C++ tutorial, but thanks to a bug, I have found a strange behaviour of C++ classes.
When defining in a class a virtual destructor, it will be automatically called when its heirs' destructor are called.
Example :
Code:
class C
{
public:
virtual ~C ()
{
cout << "C desctructor" << endl;
}
virtual void foo ()
{
cout << "C foo" << endl;
}
};
class D : public C
{
public:
~D ()
{
cout << "D desctructor" << endl;
}
void foo ()
{
cout << "D foo" << endl;
}
};
int main ()
{
D d;
d.foo ();
return 0;
}
Code:
D foo
D destructor
C destructor
But in virtual features(excluding destructor), the base feature is not automatically called, you have to do it manually, and that's normal, because you need flexibility when you redefine a feature. But don't call the parent's destructor in the destructor of a class, because it will be called twice!
So be careful with that!!
--
Globos