| |

VerySource

 Forgot password?
 Register
Search
Author: 心碎飞了

About virtual destructor

[Copy link]

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2021-3-9 00:45:01
| Show all posts
firegunIf the destructor does not use virtual, then
Base *p = new Derive();
delete p;
Here, delete p deletes the Base component in the Derive class, but cannot delete Derive members. Therefore, in order to delete correctly, you must use the virtual destructor. For details, please refer to effective c++ 3td

//Mr. Wusan said this paragraph very clearly. Two are not one concept
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Unknown

Post time: 2021-3-9 01:15:02
| Show all posts
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?
==============
     The destructor of a derived class will automatically call the destructor of the base class at the end. This is the same as the constructor of a derived class will automatically call the no-argument constructor of the base class under certain conditions.
     If a base class destructor is set to virtual, then the derived class does not mean that it will inherit the destructor of the base class, but will inherit the "virtual" mechanism, and the destructor of the derived class will also be set to virtual. At this point, when A base class pointer actually points to the derived class. When deleting, what happens is dynamic binding, and the destructor of the derived class is called correctly, but the destructor of the derived class will only deconstruct the newly added part of the derived class. The inherited part of the base class is destructed by the destructor of the base class, and the destructor of the base class is automatically called when the destructor of the derived class is executed.
      On the contrary, if the base class destructor is not virtual, the derived class destructor is not virtual. When a base class pointer actually points to the derived class, when delete, what happens is static binding, and the compiler is based on the pointer The type is directly bound to the base class destructor, and the destructor of the naturally derived class will not be called.

    Not all destructors of classes used for inheritance need to be set to virtual, only the destructors of base classes with polymorphic purposes are set to virtual.
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2021-3-9 02:15:01
| Show all posts
When a base class pointer actually points to a derived class
==>
When a base class pointer actually points to an object of a derived class
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2021-3-9 02:30:01
| Show all posts
Please refer to Effective C++,
Look at the following situation:
base *b = new derive();
delete b;
This code should be legal and the compilation can be passed, but the behavior is not what we expected.
We expect delete to delete all resources occupied by the derive object, but if we do not add to the ~base() function
In the case of virtual, the actual behavior of delete is only to release the base part of the object, and the derive-specific part of the object will
Not being released becomes a hidden danger of resource leakage.
Adding virtual is to prompt delete to call the destructor of the derived version.
Reply

Use magic Report

1

Threads

13

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2021-3-9 04:00:02
| Show all posts
Thank you all! I have benefited a lot from it!

Regarding the question of "adding virtual before the destructor", based on your advice and the book, I think if there is nothing wrong in my understanding, it looks like this:

///////////////////////////////////////////////// ////////////////////////////////
  First of all, each derived class object has a pointer to the virtual function table. Any virtual function is accessed indirectly through this pointer. The reason for using the virtual function table is because which version of a virtual function is called is running The process (the object pointed to by the pointer at the time of the call) can be determined (dynamic binding).
  Compared with virtual functions, the calling mechanism of real functions is much simpler: since each real function has only one version, the calling address can be determined at compile time (static binding)

  The destructor can also be called a virtual function through virtual modification. The difference between a virtual destructor and a general virtual function is:
  1 "Its redefinition function is the destructor of the derived class, but the same name is not required.
  2" After a version of a virtual destructor is called, it is then necessary to call the execution base class version, and so on, until the first version of the virtual destructor that executes the derived sequence is called.

///////////////////////////////////////////////// ////////////////////////////////
  The above is quoted from "National Computer Rank Examination Level Two Course-C++ Language Programming"

In other words, the object produced by Base *p=new Derive; is Base.

  When virtual is not added before the base class, the destructor of the derived class is not added to the virtual function table. At this time, the destructor is a real function, and there is only one version, which is the object's own ~Base();

  When virtual is added before the base class, the destructors of the derived class and the base class are added to the virtual function table. When the destructor is called, the destructor of the version pointed to by the current pointer is called. The next thing: a virtual After the version of the destructor is called, the base class version is then called, and so on, until the first virtual destructor version that executes the derived sequence is called.

5555~ It's not easy~
I want to say something-long live understanding!! Thank you so much!!!

Finally, please help everyone to find out what is wrong with the above ^-^
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2021-3-9 05:15:01
| Show all posts
Asking this kind of question, you can see that the program is written too little
Many things will naturally come across when you write a program. At that time, your understanding is the deepest! !
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2021-3-9 06:15:02
| Show all posts
When the pointer of the base class needs to call the derived class object, the virtual function of the base class needs to be declared as virtual, and the objects of the base class and the derived class can be deconstructed during destructuring.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2021-3-9 07:00:02
| Show all posts
Haha, the destructor in DELPHI has been doing this all the time.
Reply

Use magic Report

0

Threads

55

Posts

44.00

Credits

Newbie

Rank: 1

Credits
44.00

 Invalid IP Address

Post time: 2021-3-9 08:00:01
| Show all posts
The original poster, the destructor cannot be inherited, but the compiler will automatically generate the destructor (see effective c++ for specific conditions). This automatically produced destructor will call the destructor of the base class.
Reply

Use magic Report

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2021-3-9 08:15:01
| Show all posts
In order to pair construction and destruction, if there is no virtual, once inheritance occurs, there will be a pairing of construction (constructor of subclass) and destruction (destruction of parent class), which should not appear
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