Why do destructors have to be virtual




















Here's a simplified rule of thumb that usually protects you and usually doesn't cost you anything: make your destructor virtual if your class has any virtual functions. Rationale: that usually protects you because most base classes have at least one virtual function. In other words, you've already paid all the per-object space-cost that you'll ever pay once you add the first virtual function, so the virtual destructor doesn't add any additional per-object space cost.

Everything in this bullet is theoretically compiler-specific, but in practice it will be valid on almost all compilers. Note: in a derived class, if your base class has a virtual destructor, your own destructor is automatically virtual. You might need an explicitly defined destructor for other reasons, but there's no need to redeclare a destructor simply to make sure it is virtual. 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. Tunvir Rahman Tusher Tunvir Rahman Tusher 5, 2 2 gold badges 33 33 silver badges 30 30 bronze badges.

Construction of derived object must follow the chain of construction from derived to base. So you need not write the virtual keyword for your constructor. Thanks — Tunvir Rahman Tusher. Murkantilism, "virtual constructors cannot be done" is true indeed.

A constructor cannot be marked virtual. See parashift. TunvirRahmanTusher could you please explain why the Base Destructor is called?? Add a comment. Bill the Lizard Bill the Lizard k gold badges silver badges bronze badges. If not, would it make sense to have the compiler check this condition and issue an error is it is not satisfied?

Giorgio I don't know of any exceptions to the rule. A compiler warning or a warning from a static analysis tool makes sense to me. Classes can be designed not to be deleted through a certain type's pointer, yet still have virtual functions - typical example is a callback interface. One does not delete his implementation through a callback interface pointer as that's only for subscribing, but it does have virtual functions.

Since we're just quoting people here, I prefer Sutter from above: "Guideline 4: A base class destructor should be either public and virtual, or protected and nonvirtual.

Of course the scope is quite limited. Show 2 more comments. Community Bot 1 1 1 silver badge. BigSandwich BigSandwich 2, 2 2 gold badges 21 21 silver badges 26 26 bronze badges. I had a look at that question of yours and saw that you had declared the base destructor as virtual. So does "deleting a base class pointer when there is no virtual destructor will result in undefined behavior" stay valid with respect to that question of yours?

Since, in that question, when you called delete, the derived class created by its new operator is checked for a compatible version first. Since it found one there, it was called. So, don't you think it would be better to say as "deleting a base class pointer when there is no destructor will result in undefined behavior"? Thats pretty much the same thing. The default constructor is not virtual. BigSandwich "hang myself"? You mean memory leaking?

Make the destructor virtual whenever your class is polymorphic. Dana Abstract class with non-virtual destructor If you are not going to delete object through a pointer to its base class - then there is no need to have a virtual destructor. Abyx Abyx Or is that implied by the language making it safe to omit?

Wallacoloo no, only declare it when it's necessary. Am I right? Not have the base virtual destructor and calling delete on a base pointer leads to undefined behavior. JamesAdkison why does it lead to undefined behaviour?? I don't have a copy but the link takes you to a comment where someone references the location within the standard. Indeed, the language requires it - 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.

Mukul Kashmira Mukul Kashmira 1 1 silver badge 7 7 bronze badges. Dragan Ostojic Dragan Ostojic 99 1 1 silver badge 1 1 bronze badge. It's a different perspective on the same question. If we think in terms of interfaces instead of base class vs derived class then it's natural conclusion: if it's a part of the interface than make it virtual. If it's not don't. Regarding destructor is expected to be implemented : that is often unnecessary.

Unless a class is managing a resource such as raw dynamically allocated memory e. And note that if a destructor or any function is declared virtual in a base class, it is automatically virtual in a derived class, even if it is not declared so. This misses the crucial detail that the destructor is not necessarily part of the interface.

Then a virtual destructor has no purpose. Of course, to ensure this, the non-virtual - probably default - destructor should be non-public. Trantor Trantor 1 1 gold badge 8 8 silver badges 26 26 bronze badges.

Why we need a virtual destructor See the following sample with some virtual functions The sample also tell how you can convert a letter to upper or lower include "stdafx. See the next sample with the virtual destructor include "stdafx. Without virtual it will print out: This is A. And now you should understand when to use virtual destructors.

Peter Hall Also, why the unnecessary dynamic allocation here? Checking for NULL - which should be nullptr - before delete ing - with incorrect indendation - is not required: delete nullptr; is defined as a no-op. It is safe to call delete on a NULL pointer i. SaileshD Yes, I know. That is what I said in my comment — James Adkison. These objects are commonly allocated using a user-defined new operator that takes a placement argument.

The delete operator cannot deallocate this memory because it is not allocated from the free store for more information, see The new and delete Operators. A call to the destructor, however, can perform appropriate cleanup.

To explicitly call the destructor for an object, s , of class String , use one of the following statements:. The notation for explicit calls to destructors, shown in the preceding, can be used regardless of whether the type defines a destructor. This allows you to make such explicit calls without knowing if a destructor is defined for the type.

An explicit call to a destructor where none is defined has no effect. A class needs a destructor if it acquires a resource, and to manage the resource safely it probably has to implement a copy constructor and a copy assignment. If these special functions are not defined by the user, they are implicitly defined by the compiler. The implicitly generated constructors and assignment operators perform shallow, memberwise copy, which is almost certainly wrong if an object is managing a resource.

In the next example, the implicitly generated copy constructor will make the pointers str1. Explicitly defining a destructor, copy constructor, or copy assignment operator prevents implicit definition of the move constructor and the move assignment operator.

In this case, failing to provide move operations is usually, if copying is expensive, a missed optimization opportunity. Feedback will be sent to Microsoft: By pressing the submit button, your feedback will be used to improve Microsoft products and services. Privacy policy. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode.



0コメント

  • 1000 / 1000