This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: missing warning


Alexander V. Lukyanov wrote:

> In the following C++ program the ~B destructor is not called (egcs-1.1). I
> know why, but none the less I beleave a warning is required.

> #include <stream.h>
> class A
> {
> public:
>    virtual void func() = 0;

// error #1: missing virtual dtor

> };
> class B : public A

// error #2: inheriting from class w/o virtual dtor

> {
> public:
>    void func() {};
>    virtual ~B() { cout << "~B\n"; }
> };
> int main()
> {
>    A *a=new B();
// error #3: up-casting a class w/o virtual dtor
>    delete a;
// error #4: deleting    "     "     "
>    return 0;
> }

This is terribly bad, and IMO should be detected by the 
compiler (-Wall). But where is the error ?

All four error positions represent a place where something 
wrong is going on. Which one should be diagnosed ?

We could try #1 with the rule:
  a base class whose dtor is public should have a virtual dtor

but how do we know that a class is a base class ?
  - cause it is abstract
  - cause it has virtual functions

position #2:
  a class with a public non virtual dtor shouldn't be used 
  as a base class

this one is easier, because we don't have to determine 
which classes are _intended_ to become base classes.

position #3: possible, but a bit late IMHO

position #4: hum, no. We have no ways at this point to 
determine the dynamic type of the object. All we could 
do is:
- to rely on flow analysis (rather weak)
- to guess that the dynamic type might be different iff the
  class was intended for derivation (see above error #1)

I am note sure about this one:

struct A {};

class B : A
{
};

do you think that this one should get a warning ?

Last question: do you care at all ? Are you too busy to 
get the compiler right ? Do you have time to make it 
clever (esp. when the user is stupid) ?

-- 

Valentin Bonnard                mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://pages.pratique.fr/~bonnardv/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]