This is the mail archive of the gcc@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]
Other format: [Raw text]

Re: Question w.r.t. `'class Foo' has virtual functions but non-virtual destructor` warning.


On Fri, Mar 04, 2005 at 03:51:42PM +0100, Karel Gardas wrote:

> I would like to ask if the behaviour of GCC 4.0.0 20050301 is correct or
> not. I have for example abstract base class like:
> 
> class Foo
> {
> public:
>     virtual unsigned short
>     iiop_version() const = 0;
> };
> 
> and when I compile it, GCC emits warning from subject, although this class
> is really abstract and will never be instantiated. It's quite easy to add
> virtual dtor there, but I'm reluctant to do so, since IMHO GCC should
> check if the class is abstract or not, so I would like to ask if I should
> fill a bugreport or correct my code.

If it's an Abstract Base Class and you intend to use it through a
pointer-to-base (or reference-to-base) then it's important that you add
a virtual dtor. 

Whether it is abstract or not has nothing to do with it - if you will
ever delete a derived object with a static type of the base class then
the behaviour is undefined if the base class does not have a virtual
dtor.

e.g. this is undefined behaviour:

class Base {};
class Derived : public Base {};

Base* p = new Derived;
delete p;

To quote the Holy Standard:

5.3.5 [expr.delete]

    -3- In the first alternative (delete object), if the static type of
    the operand is different from its dynamic type, the static type
    shall be a base class of the operand's dynamic type and the static
    type shall have a virtual destructor or the behavior is undefined.
    In the second alternative (delete array) if the dynamic type of the
    object to be deleted differs from its static type, the behavior is
    undefined.

I suggest you add "virtual ~Foo() {}" to your class.

jon


-- 
"Sell your cleverness and buy bewilderment:
 Cleverness is mere opinion, bewilderment is intuition."
	- Jalal-uddin Rumi


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