473,809 Members | 2,809 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calling base class destructor's

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?

Apr 21 '06 #1
3 3151
Read "pd->mf()" instead of "d.mf()" in h()

Apr 21 '06 #2
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

Apr 21 '06 #3
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
Apr 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1818
by: Dave | last post by:
Hi, Does the destructor of a derived class will invoke base class destructor implicitly? D
27
2139
by: tuvok | last post by:
Is it correct that the virtual dtor of base gets called implicitly? Here's some code to demonstrate what I mean: Class B has a virtual destructor, so has class D which is derived from B. Deleting D calls the dtor of D and then the dtor of B. I was thinking that this would be true only for non-virtual dtor case, but I wouldn't have expected it happen for a virtual dtor. For a class with a virtual dtor I would have expected that only the...
20
2758
by: frs | last post by:
For memory economization, I need to get rid if the virtual destructor. Under this constraint I get caught up in a design, where I need to call a destructor of a derived class from a base class. Briefly it could be displayed like the code below. It ends up deleting the member 'x' of 'B' twice. Why? Is there a way around? Thanks Frank
6
2109
by: Justin | last post by:
Hello, first time posting. If I have a base class and a derived class, is there only one way to call the base constructor? i.e. Is this the only way I can call the base constructor (presuming the base constructor took an int): Derived::Derived(int a) : Base(a)
3
2238
by: esafran | last post by:
I've defined a class, but now I want to Define a Finalizer (destructor)... How do I call the Base Finalizer???, void Finalize() is a protected override method and Type.GetType Does not work. Hoe do I call it? private void DefineFinalizer(TypeBuilder typeBuilder) {
7
2366
by: Lloyd Dupont | last post by:
I create 2 ManagedC++ class inherithing from each other. I can't see how do I call super destructor or ensure it's called :-/ template <class T> public ref class CArray : System::IDisposable { protected: ~CArray() { if( ptr ) // I'm not sure of what IDisposable will do... free( ptr );
3
1918
by: marcwentink | last post by:
Say I have a class A, and a class B that inherits from A. Now A (and B) has a virtual destructor and a virtual function F(); If I now make these statements A* ptrA = new B; ptrA->F(); delete ptrA then in the statement ptrA->F(), by means of the polymorph behavior,
6
5644
by: Henrik Goldman | last post by:
Hi I've had a problem with gcc mac osx which I think I figured out. I would like to double check with people here to see if my understanding is correct: I have a class A which class B inherit from. A has a pure virtual function virtual in f1() = 0. In B I implement this function and the compiler works out the code. However from A's destructor B can be compiled and then the runtime aborts with saying that a call to a pure virtual...
1
2529
by: Rune Allnor | last post by:
Hi all. I am sure this is an oldie, but I can't find a useful suggestion on how to solve it. I have a class hierarchy of classes derived from a base class. I would like to set up a vector of smart pointers to the base class, and access the virtual functions of any class in the hierarchy through the virtual functions.
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10639
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7661
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5688
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.