Destructor in C ++

I had a constructor in the AB.h file:

class AB{ private: int i; public: AB:i(0){}//constructor ~AB:i(0){} // destructor virtual void methodA(unsigned int value)=0;}; 

The compiler said that:

class AB has virtual functions, but not a virtual destructor
AB.h: In the destructor 'AB :: ~ AB ():
AC.h: error: only constructors accept base initializers

if I use ~ AB (); destructor, he said that I have virtual functions, but I didnโ€™t have a destructor, where did I misunderstand? Thankyou

+4
source share
9 answers

You receive an error message and an unrelated warning.

The error is due to the fact that you are using an initializer for your destructor, which does not make sense and is not a valid syntax.

Do you want to:

 ~AB() { } // destructor 

A warning is that you did not declare your destructor virtual. Classes with virtual methods must have virtual destructors :

 virtual ~AB() { } 
+6
source

Using an initialization list like

 AB : a_member(4),another_member(5) {} 

only makes sense (and is allowed) for constructors - in the destructor you don't want to initialize things.

In addition to this obvious syntax error, the compiler warns because AB has a virtual method, but does not declare it a virtual destructor. This is recommended because of the following:

 AB* ab = new SomethingDerivedFromAB(); delete ab; // calls only AB dtor and not SomethingDeriveFromAB unless AB declares its dtor virtual 
+9
source

If you have virtual functions, this is an indicator that the class should be a subclass.

If you do not declare the destructor as virtual, a person with a pointer to the base class can call the destructor, which will not call the subclass's destructor, because it is not a virtual destructor. Such a condition will leave the memory / elements controlled by the subclass in an undefined state, since they will not be properly cleared.

The lesson to be learned is to always do a virtual destructor, even if you already provide an implementation.

+2
source

You must use virtual ~AB { } or protected: ~AB { } for your destructor.

Member initialization list : var(whatever) is for building objects and does not make sense in the destructor.

The warning about virtuality is due to the fact that in general, if you plan to use your class polymorphically, you want it to be also deleted due to polymorphism. Otherwise, protect the destructor so that you cannot polymorphically destroy your objects from the parent pointer.

+1
source

The destructor must be virtual, since you plan to inherit from this class (method A is virtual). However, this is just a warning.

The error is that the destructor has no argument, and obviously does not have an initializer.

 virtual ~AB() {} 
+1
source
 class AB{ private: int i; public: AB:i(0){} virtual ~AB(){} virtual void methodA( unsigned int value ) = 0 ; }; 
+1
source

For a virtual destructor, see Rule of Three .

+1
source

Missing brackets in the destructor: Oo

 class AB { private: int i; public: AB():i(0){} // <-- parentheses here virtual ~AB() {} // <-- parentheses here virtual void methodA(unsigned int value)=0; }; 
+1
source

The compiler warns you that although you have a virtual method in your class, your destructor is not virtual. This can lead to problems, because in your program there may be a point where only the base destructor will be called.

0
source

Source: https://habr.com/ru/post/1344777/


All Articles