Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > calling base class destructor's

Reply
Thread Tools

calling base class destructor's

 
 
Raider
Guest
Posts: n/a
 
      04-21-2006
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?

 
Reply With Quote
 
 
 
 
Raider
Guest
Posts: n/a
 
      04-21-2006
Read "pd->mf()" instead of "d.mf()" in h()

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      04-21-2006
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

 
Reply With Quote
 
Tom Widmer
Guest
Posts: n/a
 
      04-21-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Base class method Need base class value Karan Rajput Ruby 2 12-22-2010 04:47 PM
base class public type (non template and template base class) Hicham Mouline C++ 1 04-20-2009 03:28 PM
Access of base class' private base class: qualification required, why Alf P. Steinbach C++ 6 09-03-2005 04:03 PM
calling virtual function results in calling function of base class... Andreas Lagemann C++ 8 01-10-2005 11:03 PM
calling virtual function results in calling function of base class ... tiwy C++ 0 01-09-2005 11:17 PM



Advertisments