开发者

destructor in C++

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;}开发者_如何学Go;

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


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


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() { }


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.


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.


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() {}


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


About the virtual destructor, see the Rule of Three.


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;
};


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜