|
It seems that my language skills really need to be strengthened -_-b
The constructor and destructor derived classes of the base class written in the book cannot be inherited!
Let me ask again (hey~~):
Why if there is no virtual, delete p; call one less destructor?
I understand virtual in this way:
A derived class can inherit all members and functions from the parent class except the constructor and destructor, such as:
class Base
{
public:
Base();
~Base(){cout<<"Destructor Base!";};
virtual void Out1(){cout<<"In the Base!(Out1)";};
void Out2(){cout<<"In the Base!(Out2)";};
private:
int x;
int y;
}
class Derived:public Base
{
public:
Derived(int z);
~Derived(){cout<<"Destructor Derived";};
virtual void Out1(){cout<<"In the Derived!(Out1)";};
void Out2(){cout<<"In the Derived!(Out2)";};
void Out3(){cout<<"In the Derived!(Out3)";};
private:
int z;
};
int main()
{
Base bb,*p;
Derived dd(123);
*p=bb;
*p->Out1(); //In the Base!(Out1)
*p->Out2(); //In the Base!(Out2)
*p=dd;
*p->Out1(); //In the Derived!(Out1)
*p->Out2(); //In the Base!(Out2)
delete p;
return 0;
};
The Derived above inherits the addition of Base
Base(); ~Base();
All the members (x, y) and functions (Out1, Out2) outside the two functions, when I define a base class pointer to access the derived class object to achieve polymorphism, but because it is a base class pointer, so You can only access the functions of the derived class inherited from the base class (such as Out1, at this time there is no virtual before Out2 in Derived, although its name is the same as Out2 of Base, but it is hidden by Out2 of Derived in Derived). Access Derived's own newly defined functions (such as Out2, Out3, of course, including Dervied's constructor and destructor).
Well, since *p can access Out1 of the derived class and Out1 of the base class, you can assign different objects (bb, dd) to *p to call Out1 of different objects to achieve polymorphism.
But it won’t work for Out2, because although *p=bb points to the object of the derived class, *p->Out2(); the original intention is to call Out2 of the derived class, but because Out2 is a newly added function in the derived class, The pointer of the base class cannot be accessed, so when I use *p->Out2();, the compiler will find the hidden function Out2() of the same name inherited from the base class in the derived class;
The understanding below feels a problem! ! ! !
Similar to the principle of Out2() above, when I call delete p; because p is a pointer of the base class type, although it now points to the derived class object, it cannot find the analysis inherited from the base class in the derived class. Constructor (constructor and destructor cannot be inherited) and ~Derived() is a new function of the derived class. The base class pointer has no right to access, so it can’t be called. Delete p; can only skip this destructor Function, call the destructor of the base class (at this time, because z is a member of the derived class, calling the destructor of the base class can not release the space of z! Caused an error)
And when I add virtual in front of the destructor of the base class...there is no destructor with the same name in the derived class, what's the use of adding it, but it seems that the destructor does not require the same name... . The mess is messed up, I don’t know the rest
1>>>>What I understood above, right?
2>>>> Why if there is no virtual, delete p; call one less destructor?
3>>>>Why do delete p; call two destructors if there is virtual?
Thank you everyone, I have written a lot, because I really want to know what happened behind me |
|