Boost logo

Boost :

From: Jonathan Wakely (cow_at_[hidden])
Date: 2005-02-28 03:24:42


On Sun, Feb 27, 2005 at 01:11:11PM -0500, christopher diggins wrote:

> From: "Jeff Garland"
> >
> >At the risk of going against the entrenched dogma here, I'd suggest there
> >are
> >significant cases where this is a good and useful thing to do. I think the
> >original guidance came from Scott Meyers Effective C++ #14 -- make
> >destructors
> >virtual in base classes. The problem I have with this is that I find
> >there
> >are plenty of situations where I might want to add some convenience
> >functions
> >to an stl container in which the subclass has a trivial destructor (eg: no
> >allocated resources)
>
> I am a little confused here, does MySubClass below have a "trivial"
> destructor?

(N.B. I think the standard uses "trivial destructor" in a stricter sense,
meaning that the class *and* all its members and bases have
implicitly-declared dtors.)

> MySubClass : MyBaseClass {
> int x;
> };

I'm not sure what happens to x. I know that with GCC the following code
never calls B::~B(), so if it was doing any non-trivial work it would
leak:

    #include <iostream>

    struct A {};
    struct B
    {
        ~B() { std::cout << "B::~B\n"; }
    };
    struct C : A
    {
        B b;
    };

    int main()
    {
        A* p = new C;
        delete p;
    }

That implies that if you derive from a concrete type with a non-virtual
dtor you'd better not add any data members at all. I think don't many
people realise that, so I don't think encouraging inheritance from
STL containers is a good idea.

I might be wrong, but does 12.5/4 mean it is ill-formed to call delete
on a pointer-to-base if base::~base is not virtual?

jon

-- 
When writing a specialization
Be careful about its location
Else to make it compile
Will be such a trial
As to kindle its self-immolation
	- ISO/IEC 14882/1998 (The C++ Standard)

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