Why destructor is virtual in c




















Machine Learning. Data Structures. Operating System. Computer Network. Compiler Design. Computer Organization. Discrete Mathematics. Ethical Hacking. Computer Graphics. Software Engineering. Web Technology. Cyber Security. Inline assembly. Run this code. Compiler support. Freestanding and hosted. Language support library.

Technical specifications. Flow control. Function declaration. Lambda function declaration. Fundamental types. Function types. Compound types. Storage duration specifiers.

Default initialization. Value initialization. Zero initialization. Copy initialization. Direct initialization. Aggregate initialization. Constant initialization. Reference initialization. Value categories. Order of evaluation. Operator precedence. If you pass this a or b pointer to delete it will crash due to the address being invalid to the heap's free routine.

If you plan to delete derived instances which have vtables from base class pointers, you need to ensure the base class has a vtable.

One way to do that is to add a virtual destructor, which you might want anyway to properly clean up resources. A basic definition about virtual is it determines if a member function of a class can be over-ridden in its derived classes. A class's D-tor is called basically at the end of the scope, but there is a problem, for example when we define an instance on the Heap dynamic allocation , we should delete it manually.

As soon as the instruction get executed, the base class destructor get called, but not for the derived one. At the end of the scope, if the destructor of one of the power elements Actuator , isn't called, there will be fatal consequences. I think most answers here miss the point, with the exception of the accepted one, which is a good thing. However, let me add one more with a different view on the issue: You need a virtual destructor if you want to polymorphically delete instances of this class.

However, there are several assumptions there that need to be make explicit:. For a similarly different view on the subject, also read When should you not use virtual destructors?

Any class that is inherited publicly, polymorphic or not, should have a virtual destructor. To put another way, if it can be pointed to by a base class pointer, its base class should have a virtual destructor.

If virtual, the derived class destructor gets called and then the base class destructor. If not virtual, only the base class destructor gets called. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. When to use virtual destructors? Ask Question. Asked 12 years, 9 months ago. Active 1 month ago. Viewed k times. When are you meant to make them virtual and why?

Improve this question. Lodle Lodle See this: Virtual Destructor — Naveen. Every destructor down gets called no matter what. I'm also confused by MooingDuck 's answer. Shouldn't it be up instead of down , if you use the notion of subclass under and superclass above? Nibor: Yes, if you use that notion. About half the people I talk to view superclasses as "above", and half view superclasses as "below", so both are conflicting standards, which makes everything confusing. I think superclass as "above" is slightly more common, but that's not the way I was taught : — Mooing Duck.

Show 5 more comments. Active Oldest Votes. Improve this answer. Luc Touraille Luc Touraille This would explain why i had massive leaks using a factory i made before. All makes sense now. Thanks — Lodle. Well, this is a bad example as there are no data members. What if Base and Derived have all automatic storage variables? Is it ok then to leave off writing any destructors at all? Or will the derived class still have a memory leak? Wait, it will be undefined behavior — bobobobo.

From the Herb Sutter's article: "Guideline 4: A base class destructor should be either public and virtual, or protected and nonvirtual.

Also from the article - 'if you delete polymorphically without a virtual destructor, you summon the dreaded specter of "undefined behavior," a specter I personally would rather not meet in even a moderately well-lit alley, thank you very much. Show 8 more comments. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. What's New. Most popular in C Language.

We use cookies to ensure you have the best browsing experience on our website.



0コメント

  • 1000 / 1000