George2

Hello everyone,


What is the purpose of virtual destructor If currently, no derived class


thanks in advance,
George



Re: Visual C++ Language virtual destructor

einaros

The purpose is to allow safe derivation from that base class. If there's no derived class, or an instance of the base class is created, there's no effect of it being virtual (other than a possible size increase because of a virtual table being initialized).





Re: Visual C++ Language virtual destructor

Sreedhar DVS

Virtual distructor is essential if you implement inheritance. It's used in base class. It guarantees that for all classes derived from the base class all destructors will be called (in proper order).

There is no use of using virtual distructor if you dont have any derived class.






Re: Visual C++ Language virtual destructor

George2

Thanks einaros,

einaros wrote:
The purpose is to allow safe derivation from that base class.

Could you describe more what do you mean safe derivation please A simple sample

regards,

George





Re: Visual C++ Language virtual destructor

George2

Thanks Sreedhar,

Sreedhar DVS wrote:

Virtual distructor is essential if you implement inheritance. It's used in base class. It guarantees that for all classes derived from the base class all destructors will be called (in proper order).

Could you describe in more details why virtual destructor will guarantee all classes derived from the base class all destructors will be called (in proper order) please Compared with non-virtual destructor

regards,

George





Re: Visual C++ Language virtual destructor

einaros

George2 wrote:

einaros wrote:
The purpose is to allow safe derivation from that base class.

Could you describe more what do you mean safe derivation please A simple sample



The problem arises when you treat the types polymorphically:

class Base
{
public:
~Base()
{
}
};

class Derived : public Base
{
public:
~Derived()
{
// Do some very important cleanup here
}
};

Base* ptr = new Derived();
delete ptr; // Since the Base destructor is not virtual, the derived Destructor will not be called





Re: Visual C++ Language virtual destructor

George2

Thanks einaros,

The sample is great!

I think you mean if the destructor is virtual, then if we delete an object of derived class through based class pointer, then the derived class destructor is invoked Right

If the destructor is non-virtual, then if we delete an object of derived class through based class pointer, then the base class destructor is invoked Right


regards,
George

einaros wrote:

George2 wrote:

einaros wrote:
The purpose is to allow safe derivation from that base class.

Could you describe more what do you mean safe derivation please A simple sample



The problem arises when you treat the types polymorphically:

class Base
{
public:
~Base()
{
}
};

class Derived : public Base
{
public:
~Derived()
{
// Do some very important cleanup here
}
};

Base* ptr = new Derived();
delete ptr; // Since the Base destructor is not virtual, the derived Destructor will not be called




Re: Visual C++ Language virtual destructor

einaros

George2 wrote:

I think you mean if the destructor is virtual, then if we delete an object of derived class through based class pointer, then the derived class destructor is invoked Right


If you delete an object with a virtual destructor, through a base instance, the most derived destructor will be called first, then the second-last destructor, the third-last destructor and so froth -- right up to the base destructor.






Re: Visual C++ Language virtual destructor

George2

Thanks einaros,

It is interesting. You mean if the destructors are virtual, even if they do not have explicit invocation relationship in code (e.g. the most derived destructor will call the second-last destructor explicitly in source codes), the second-last destructor, the third-last destructor and so forth will be invoked automatically

But if the destructor is not virtual, we do not have this feature

regards,

George

einaros wrote:

George2 wrote:

I think you mean if the destructor is virtual, then if we delete an object of derived class through based class pointer, then the derived class destructor is invoked Right


If you delete an object with a virtual destructor, through a base instance, the most derived destructor will be called first, then the second-last destructor, the third-last destructor and so froth -- right up to the base destructor.





Re: Visual C++ Language virtual destructor

einaros

George2 wrote:

You mean if the destructors are virtual, even if they do not have explicit invocation relationship in code (e.g. the most derived destructor will call the second-last destructor explicitly in source codes), the second-last destructor, the third-last destructor and so forth will be invoked automatically



No, this is not caused by the destructor being virtual; it's the destruction order of any derived class hierachy which is not deleted polymorphically. Virtual destructors are just like virtual functions, that's all there's to it.

Example, completely disregarding (the here unnecessary) virtual dispatch:

class A {}
class B : public A {};
class C : public B {};

C* c = new C(); // Will call A's ctor, then B's ctor and finally C's ctor
delete c; // Will call C's dtor, then B's dtor and finally A's dtor






Re: Visual C++ Language virtual destructor

George2

Thanks einaros,

I want to confirm with you that you mean whether or not the destructor is virtual, it does not matter. And your class A, B, C sample describe a general behavior which does not have to do with specific virtual function, right

regards,

George

einaros wrote:

George2 wrote:

You mean if the destructors are virtual, even if they do not have explicit invocation relationship in code (e.g. the most derived destructor will call the second-last destructor explicitly in source codes), the second-last destructor, the third-last destructor and so forth will be invoked automatically



No, this is not caused by the destructor being virtual; it's the destruction order of any derived class hierachy which is not deleted polymorphically. Virtual destructors are just like virtual functions, that's all there's to it.

Example, completely disregarding (the here unnecessary) virtual dispatch:

class A {}
class B : public A {};
class C : public B {};

C* c = new C(); // Will call A's ctor, then B's ctor and finally C's ctor
delete c; // Will call C's dtor, then B's dtor and finally A's dtor





Re: Visual C++ Language virtual destructor

einaros

George2 wrote:

I want to confirm with you that you mean whether or not the destructor is virtual, it does not matter. And your class A, B, C sample describe a general behavior which does not have to do with specific virtual function, right


Correct, that example displays normal construction and destruction order, and disregards virtual destructors.

Virtual destructors is necessary for polymorphic deletion, as noted in the above posts, not to make construction and destruction work properly in non-polymorphic cases.





Re: Visual C++ Language virtual destructor

George2

Thanks for all the help, einaros!

have a good weekend,

George

einaros wrote:

George2 wrote:

I want to confirm with you that you mean whether or not the destructor is virtual, it does not matter. And your class A, B, C sample describe a general behavior which does not have to do with specific virtual function, right


Correct, that example displays normal construction and destruction order, and disregards virtual destructors.

Virtual destructors is necessary for polymorphic deletion, as noted in the above posts, not to make construction and destruction work properly in non-polymorphic cases.