Boost logo

Boost :

From: Emil Dotchevski (emil_at_[hidden])
Date: 2008-05-03 22:49:24


On Sat, May 3, 2008 at 1:13 PM, Hartmut Kaiser <hartmut.kaiser_at_[hidden]> wrote:
> > Stefan Seefeld:
> > > How do you prevent then any users deriving from your classes, from
> > > adding their own destructors ? Their destructors might not be
> > invoked,
> > > since the base destructor isn't virtual.
> >
> > How does this look in code?
>
> struct A
> {
> ~A() {} // non-virtual
> virtual void foo() {}
> };
>
> struct B : A
> {
> ~B() {}
> void foo() {}
> };
>
> ///////////
> A* a = new B;
> delete a; // doesn't call ~B(), but should

1) The code that generates the bogus warning never deletes object of a
derived type through a base type pointer, so your example is
irrelevant to this discussion.

2) The types that generate the warnings are not part of the public
interface of the library and users should not be deriving them.

3) In general, there are two ways to protect users from making this
type of mistake:

- One is to make the destructor public and virtual; this tells the
user that they are welcome to delete an object through the base type
pointer.

- The other is to make the destructor protected and non-virtual, which
tells the user that they are not allowed to delete an object through
the base type pointer.

Both are equally valid and therefore the compiler is wrong to issue a
warning in the latter case.

Emil Dotchevski
Reverge Studios, Inc.
http://www.revergestudios.com/reblog/index.php?n=ReCode


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk