| |

VerySource

 Forgot password?
 Register
Search
Author: 心碎飞了

About virtual destructor

[Copy link]

0

Threads

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2021-3-8 21:30:01
| Show all posts
This has nothing to do with whether the destructor can be inherited, for
Base *p = new Derive;
The static type of p is Base*, and the dynamic type is Derive*. If the virtual destructor is not declared, its static type destructor will be called, that is, the destructor of the Base class. In addition, the method of calling the destructor is the destructor of the Drive class first, and then the destructor of the Base class. This is the same with or without virtual, and if there is no virtual, delete p; one less destructor is called Function!
Reply

Use magic Report

1

Threads

39

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2021-3-8 21:45:01
| Show all posts
Destructors can be inherited. Constructors can't be inherited (or maybe not, but they can't be defined as virtual functions).

Why the destructor has to be made into a virtual function is for the correctness of the destructor order.
The construction starts from the base class to the subclass.
The order of destruction is just the opposite.
When constructing, we can clearly call the correct function, but when destructing, if it is pointed to by the pointer of the base class.
There is no mechanism for virtual functions, then the destructor of the base class is called.
It is impossible for the base class to know that the newly applied resources of the subclass have not been released.
Therefore, it is necessary to use the mechanism of virtual functions to achieve this. By accurately calling the destructor of the subclass, then
Can free up resources.
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2021-3-8 22:00:01
| Show all posts
Without understanding the original poster, the description "in order to allow subclasses to inherit" is difficult to understand. What is the concept of "inheritance"? This must be defined first.

The virtual member function can (or "for") be overridden by its derived class. The destructor can be overridden. The implementation of the destructor is automatic and mandatory to call the implementation of its base class.

The meaning of virtual is "determine the actual code executed according to the object when it is called", and has nothing to do with whether it is a destructor or not.

As long as it is a non-static member, the class has been constructed and has not been destructed when it is called, it can be virtual. The constructor meets this condition (maybe the part of its derived class has been destructed, but the part of the class it belongs to must not have started to be destructed).
Reply

Use magic Report

0

Threads

9

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2021-3-8 22:15:01
| Show all posts
The problem comes again, since adding virtual before the destructor is not to allow subclasses to inherit,
1>>>>What does Nagata do?
2>>>> Can it be said that after adding virtual, the destructor of the subclass is added to the virtual function table? (At this time, both the base class and subclass destructors are called)
Without virtual, the subclass will not be added to the virtual function table, and will not be called at the end? (At this time, only the destructor of the base class is called)

---------------------------------------------ANS---- -------------------------------------------------- -----------------------
>>>>You have already understood the role of adding virtual to prevent resource leakage when using parent-child pointer conversion. This is mentioned in Effective c++.
>>>> is not a virtual function, it will not enter the virtual function table. At this time, there is no index pointing to the destructor in the virtual function table. When the virtual function is not added, the destructor of the subclass will automatically call the destructor of the parent class. This and The extensions mentioned above are the same. At this time, the behavior of the function is different from when virtual is not added.

For the specific content of the virtual function table, see <inside c++ object modal> chap 1.1 or see <thinking in c++>. The content of the virtual destructor function is also explained in <inside c++ object modal>.

The role of virtual is to achieve "polymorphism", this is its essence.
Reply

Use magic Report

1

Threads

13

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2021-3-8 22:30:02
| Show all posts
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
Reply

Use magic Report

1

Threads

13

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2021-3-8 22:45:01
| Show all posts
4>>> (forgot to say) I saw this in Essential C++: When there are one or more virtual functions in a class, its destructor should be defined as virtual! (?? What does this have to do with the virtual function of the base class, isn't it because of the insurance that the newly defined member space in the derived class will not be released?)

If I sawbooookafter typing those words, it seems that I read too few books, and I should have said that: It doesn’t matter if I don’t understand it now, I will see more later, I will understand...
Effective C++ is in the mail, but now I really want to know, hey~ It seems that I am a contradictory person
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2021-3-8 23:15:01
| Show all posts
4: When there are one or more virtual functions in the class, its destructor should be defined as virtual
-------
If there is a virtual function in a class, the creator will tell you clearly that this class is used for inheritance. Obviously, the destructor must be virtual.


Don't be discouraged, lz, you can only see it if you have your own opinion
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2021-3-8 23:30:02
| Show all posts
If it is not a sealed class, it is best to write all the destructors as virtual. This is a good habit and can avoid many problems
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2021-3-8 23:45:02
| Show all posts
Of course, except for programs that require high execution efficiency~~
With virtual functions, there will be virtual tables, which involve dynamic binding, which will have some impact on efficiency, but the structure of the application is more important. . .
Reply

Use magic Report

0

Threads

9

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 United States

Post time: 2021-3-9 00:00:01
| Show all posts
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?

If the destructor of the base class is not virtual, then Base *p; p = new Derive; delete p; call the destructor of the base class at this time. And if the base class is virtual, then the delete p operation performs polymorphism and performs subclass destruction. You only need to understand the behavior of virtual functions. Maybe you will understand when you write too much code.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list