Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] Over-eager "virtual method but no virtual dtor" warning

Deleting the base class pointer with non-virtual dtor results in undefined behavior in C++.
Under the hood it usually means that the compiler doesn't know anything about the particular derived class when it sees a pointer to the base class. If A::~A() was virtual, the dtor call would be dispatched as a virtual method call, thus, destructor for B (and its members) will be invoked properly. Since it is not, all the compiler can do is just invoke A::~A().
Now, this might actually be what you really want (virtual dispatch is more expensive, and not every class needs to dispose anything, explicitly or implicitly). But much more often than not it's just a coding error.

On Sat, Dec 6, 2014 at 11:12 PM, Alec Teal <a.teal@xxxxxxxxxxxxx> wrote:
This is driving me mad,

Like everyone here I like warning-less code, I compile with -pedantic -Wall -Wextra as any sane person should but there's this warning that I cannot get to go away.

I cannot think of any instance where the warning is justified either.

Let me distill the use-case I have encountered this warning in and why it isn't useful.

class OnEventListener {
protected:
    OnEventListener() { }
    ~OnEventListener() { /*deregister with all emitters* }
    virtual void notifyOfEvent() =0;
    void onEvent() { notifyOfEvent(); } //not sure why I do this now I am writing it, I have this habit of wrapping virtuals, either way no harm
};

Then something like:

class Whatever: protected OnEventListener {
/*whatever*/
};

The class "whatever" gets the warning. There is no way that I can be dealing with a Whatever as a OnEventListener, because the inheritance is private.
Furthermore the destructor itself is protected so even if I did get hold of a Whatever by the OnEventListener I couldn't destroy it this way anyway.

So this leads me to state:

Even if it were "class Whatever: public OnEventListener {" the warning would be wrong.

Lets distill this even further.

class A {
protected:
    A() { }
    ~A() { }
    virtual void f() =0;
};

class B: protected A {

    void f() { }
};

How could this go wrong? What is the warning warning me of?

I suppose I could friend A (which I have actually....) from something that deals with Bs as As .... but this is at a stretch because I'd have to write some really obscure code to get this, to create Bs but never manage them....

Anyway, bug or am I missing something?

Alec
_______________________________________________
cdt-dev mailing list
cdt-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/cdt-dev


Back to the top