Home > Back-end >  C the destructor of inheritance
C the destructor of inheritance

Time:05-08

Subclass destructor, how to call the superclass destructor?

 
The class Parent
{
To the Parent () {}
}

Child: class public Parent
{
~ the Child () {}
}


Child () that can be invoked to ~ ~ the Parent ()

CodePudding user response:

I tried to let both the destructor is virtual functions, but still not transferred to, feeling is also a demon

CodePudding user response:

As virtual automatically calls,

CodePudding user response:

Don't write any code automatically calls,

CodePudding user response:

reference maguangzhi reply: 3/f
don't have to write any code automatically calls,

Don't write the virtual words, calling sequence is not correct. That is to say, does not automatically call
For example the following code
 # include & lt; Iostream> 
using namespace std;

The class Base {
Public:
~ Base () {
Cout & lt; <"Base destory" & lt; }
};
Child: class public Base {
Public:
~ the Child () {
Cout & lt; <"The Child destory" & lt; }
};
Int main (void)
{
The Base * b=new Child;
The delete b;
}
  • Related