| |

VerySource

 Forgot password?
 Register
Search
View: 6116|Reply: 32

About virtual destructor

[Copy link]

1

Threads

13

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2021-3-8 18:30:01
| Show all posts |Read mode
Virtual is added to inherit polymorphism

Doesn't it mean that destructors cannot be inherited?
Reply

Use magic Report

0

Threads

41

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2021-3-8 18:45:02
| Show all posts
Is to use the base class pointer to be able to delete derived class objects
Reply

Use magic Report

1

Threads

13

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2021-3-8 19:00:01
| Show all posts
I didn't understand the words upstairs-_-b

Let me make the question more clear:
Virtual is used to implement polymorphism for functions that can be inherited, while destructors cannot be inherited!
Reply

Use magic Report

0

Threads

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2021-3-8 19:15:01
| Show all posts
If 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
Reply

Use magic Report

0

Threads

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2021-3-8 19:30:01
| Show all posts
Article Seven
Reply

Use magic Report

0

Threads

9

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2021-3-8 19:45:01
| Show all posts
In the structure of the C++ object, if there is a virtual function, there will be a virtual function table, as everyone knows, and the destructor will be placed in the second index position of the virtual function table, and the first index position is placed in type_info , Used to support RTTI. This is the case regardless of the class. The polymorphism of the destructor is different from the ordinary function, and there is only one destructor for each class, which can guarantee the calling conditions of polymorphism, and it does not use inheritance itself. Come to realize it, so you don't need to be surprised
Reply

Use magic Report

0

Threads

9

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2021-3-8 20:15:01
| Show all posts
A Pure Virtual Destructor

Unlike ordinary member functions, a virtual destructor is not overridden when redefined in a derived class. Rather, it is extended: the lower-most destructor first invokes the destructor of its base class and only then, it is executed. Consequently, when you try to declare a pure virtual destructor, you may encounter compilation errors, or worse: a runtime crash. However, there's no need to despair--you can enjoy both worlds by declaring a pure virtual destructor without a risk. The abstract class should contain a declaration (without a definition) of a pure virtual destructor:

//Interface.h file
class Interface {
public:
virtual ~Interface() = 0; //pure virtual destructor declaration
};

Somewhere outside the class declaration, the pure virtual destructor has to be defined like this:

//Interface.cpp file
Interface::~Interface()
{} //definition of a pure virtual destructor; should always be empty

This text should be more clear
Reply

Use magic Report

1

Threads

13

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2021-3-8 20:45:01
| Show all posts
E text is not good, upstairs bothered.

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)
Reply

Use magic Report

1

Threads

13

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2021-3-8 21:00:01
| Show all posts
To add, I know that virtual is added to prevent the member memory defined by the subclass from being released when the base class pointer is destructed
Reply

Use magic Report

1

Threads

13

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2021-3-8 21:15:01
| Show all posts
But now I don’t know why this can be done
Prevent the member memory defined by the subclass from being released when the base class pointer is destructed
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