3

I had a constructor in my 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 non-virtual destructor
AB.h: In destructor ‘AB::~AB()’:
AC.h: error: only constructors take base initializers

if I use the ~AB(); destructor, it said that i have virtual functions but i didn't have destructor, where did I misunderstand? Thankyou

1
  • thank guys, now i have my lesson, virtual ~destructor(){} Commented Mar 22, 2011 at 14:34

9 Answers 9

9

Using an initialization list such as

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

makes only sense (and is permitted) for constructors - in a 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 doesn't declare it's destructor virtual as well. This is recommended because of the following:

AB* ab = new SomethingDerivedFromAB(); delete ab; // calls only AB's dtor and not SomethingDeriveFromAB's unless AB declares its dtor virtual 
Sign up to request clarification or add additional context in comments.

5 Comments

Yeah, change ~AB:i(0){} to ~AB{}
yes I knew that i don't have to initialize again in destructor, but if I remove it, the complier will say that " virtual functions butdidn't have destructor"
@user: the warning about destructor is another problem; you've need to fix it, too.
@SauceMaster since when ~AB{} is a valid syntax?
@ybungalobill: Heh, typo on my part. I missed the parentheses. ~AB(){}
6

You're getting an error and an unrelated warning.

The error is because you're using an initializer for your destructor, which doesn't make sense and isn't valid syntax.

You want:

~AB() { } // destructor 

The warning is because you haven't declared your destructor virtual. Classes with virtual methods should have virtual destructors:

virtual ~AB() { } 

Comments

2

If you have virtual functions, that's an indicator that the class is meant to be subclassed.

If you don't declare the destructor as virtual, a person with a pointer to the base class could call the destructor which won't call the sub-class's destructor because it's not a virtual destructor. Such a condition would leave the memory / items managed by the subclass in limbo as they wouldn't be properly cleaned up.

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

Comments

1

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

The member initialization list : var(whatever) is for constructing objects and doesn't make sense in a destructor.

The warning about virtualness is because in general if you intend your class to be used polymorphically, you want it to be able to be deleted polymorphically as well. Alternately make the destructor protected so you can't polymorphically destroy your objects from a parent pointer.

Comments

1

The destructor should be virtual as you are planing to inherit from this class (methodA is virtual). However, this is just a warning.

The error is that the destructor has no argument, and obviously no initializer.

virtual ~AB() {} 

Comments

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

Comments

1

About the virtual destructor, see the Rule of Three.

Comments

1

You are missing parentheses and initializing in the destructor: O.o

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.