calling base class destructor's

Discussion in 'C++' started by Raider, Apr 21, 2006.

  1. Raider

    Raider Guest

    I have library with some class having no virtual functions and non
    virtual destructor.

    I want to add new member functions to this class by creating derived
    class and then use derived class instances as a base class instances:

    // -------- library --------
    class B
    {
    ...
    }

    void f(B *b)
    {
    ...
    delete b; // calls B::~B() !
    }

    // ------- my code --------
    class D : public B
    {
    public:
    D(...) {}
    void mf(); // I have access to B's protected members!
    // no new member variables!!!
    }

    void h()
    {
    D* pd = new D(...);
    d.mf();
    f( pd );
    }

    I think it is ugly but safe. What do you think?
     
    Raider, Apr 21, 2006
    #1
    1. Advertising

  2. Raider

    Raider Guest

    Read "pd->mf()" instead of "d.mf()" in h()
     
    Raider, Apr 21, 2006
    #2
    1. Advertising

  3. Raider

    mlimber Guest

    Raider wrote:
    > I have library with some class having no virtual functions and non
    > virtual destructor.
    >
    > I want to add new member functions to this class by creating derived
    > class and then use derived class instances as a base class instances:
    >
    > // -------- library --------
    > class B
    > {
    > ...
    > }
    >
    > void f(B *b)
    > {
    > ...
    > delete b; // calls B::~B() !
    > }
    >
    > // ------- my code --------
    > class D : public B
    > {
    > public:
    > D(...) {}
    > void mf(); // I have access to B's protected members!
    > // no new member variables!!!
    > }
    >
    > void h()
    > {
    > D* pd = new D(...);
    > d.mf();


    You meant pd->mf();

    > f( pd );
    > }
    >
    > I think it is ugly but safe. What do you think?


    It is not safe. Because of the lack of virtual destructor in the base
    class, you are deleting a D as though it were a B, which is bad, bad,
    bad. This lack of virtual destructor implies that the base class B was
    not meant to be a polymorphic base class. If you can't change the
    library, you'll need to come up with some other solution, such as
    supplying a copied B object from a D object:

    B* D::GetB() const
    {
    // Treat D as B for the copy ctor
    // (assumes normal copy semantics)
    B* pb = new B( *this );
    return pb;
    }

    void Foo( D* pd )
    {
    pd->mf();
    f( pd->GetB() );
    }

    (Of course, I would prefer to return a std::auto_ptr<B> from D::GetB(),
    but you get the idea.)

    Cheers! --M
     
    mlimber, Apr 21, 2006
    #3
  4. Raider

    Tom Widmer Guest

    Raider wrote:
    > I have library with some class having no virtual functions and non
    > virtual destructor.
    >
    > I want to add new member functions to this class by creating derived
    > class and then use derived class instances as a base class instances:
    >
    > // -------- library --------
    > class B
    > {
    > ...
    > }
    >
    > void f(B *b)
    > {
    > ...
    > delete b; // calls B::~B() !
    > }
    >
    > // ------- my code --------
    > class D : public B
    > {
    > public:
    > D(...) {}
    > void mf(); // I have access to B's protected members!
    > // no new member variables!!!
    > }
    >
    > void h()
    > {
    > D* pd = new D(...);
    > d.mf();
    > f( pd );
    > }
    >
    > I think it is ugly but safe. What do you think?


    It is undefined behaviour according to the C++ standard. For your
    particular example, most compilers will tend to produce code that
    doesn't crash and doesn't leak any memory (which is obviously perfectly
    valid for undefined behaviour). Whether this is a good thing is
    debatable - I'd prefer an assertion personally.

    Tom
     
    Tom Widmer, Apr 21, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    1
    Views:
    571
    qazmlp
    Apr 10, 2005
  2. Dave
    Replies:
    2
    Views:
    4,161
    Donovan Rebbechi
    May 16, 2005
  3. frs
    Replies:
    20
    Views:
    759
    Alf P. Steinbach
    Sep 21, 2005
  4. arun
    Replies:
    2
    Views:
    547
    benben
    Jun 13, 2006
  5. Replies:
    11
    Views:
    835
    Ian Collins
    Feb 9, 2009
Loading...

Share This Page