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

Home Posts Topics Members FAQ

Problem with virtual functions called from a destructor

This is a simplified version of the situation (the actual situation
is quite more complex, but when stripped to the bare minimum, it's
like this):

class BaseClass
{
public:
virtual void cleanup();
virtual ~BaseClass() { cleanup(); }
};

The cleanup() function is not only called when destroying the object
but also at other times during the lifetime of the object, and it's
usually called through a BaseClass pointer and sometimes implemented
in a derived class.

Naturally the problem is that the BaseClass destructor will not call
the implementation of cleanup() in the derived class, only its own.
This is a bummer, but I suppose it's like it should be (because,
basically, the derived part of the object "doesn't exist" anymore
when the destructor of the base class is executed).
The only clean solution to this is to explicitly call cleanup() in
the destructor of the derived class too.

Isn't there any way of automatically making the destructor of the
derived class call its own cleanup() function without having to write
it explicitly? The problem is that this is very easy to forget, it
obviously doesn't cause any compiler errors nor warnings and the
resulting behavior of the program can be quite confusing.
Jul 10 '07 #1
3 1384
On 7 11 , 7 16 , Juha Nieminen <nos...@thanks. invalidwrote:
This is a simplified version of the situation (the actual situation
is quite more complex, but when stripped to the bare minimum, it's
like this):

class BaseClass
{
public:
virtual void cleanup();
virtual ~BaseClass() { cleanup(); }

};

The cleanup() function is not only called when destroying the object
but also at other times during the lifetime of the object, and it's
usually called through a BaseClass pointer and sometimes implemented
in a derived class.

Naturally the problem is that the BaseClass destructor will not call
the implementation of cleanup() in the derived class, only its own.
This is a bummer, but I suppose it's like it should be (because,
basically, the derived part of the object "doesn't exist" anymore
when the destructor of the base class is executed).
The only clean solution to this is to explicitly call cleanup() in
the destructor of the derived class too.

Isn't there any way of automatically making the destructor of the
derived class call its own cleanup() function without having to write
it explicitly? The problem is that this is very easy to forget, it
obviously doesn't cause any compiler errors nor warnings and the
resulting behavior of the program can be quite confusing.

Jul 11 '07 #2
On Jul 11, 3:16 am, Juha Nieminen <nos...@thanks. invalidwrote:
This is a simplified version of the situation (the actual situation
is quite more complex, but when stripped to the bare minimum, it's
like this):

class BaseClass
{
public:
virtual void cleanup();
virtual ~BaseClass() { cleanup(); }

};

The cleanup() function is not only called when destroying the object
but also at other times during the lifetime of the object, and it's
usually called through a BaseClass pointer and sometimes implemented
in a derived class.

Naturally the problem is that the BaseClass destructor will not call
the implementation of cleanup() in the derived class, only its own.
This is a bummer, but I suppose it's like it should be (because,
basically, the derived part of the object "doesn't exist" anymore
when the destructor of the base class is executed).
The only clean solution to this is to explicitly call cleanup() in
the destructor of the derived class too.

Isn't there any way of automatically making the destructor of the
derived class call its own cleanup() function without having to write
it explicitly? The problem is that this is very easy to forget, it
obviously doesn't cause any compiler errors nor warnings and the
resulting behavior of the program can be quite confusing.
you can invent a techniqe but it can not become implicit.
a popular way is this:

class baseClass{
protected:
bool clean_flag;
virtual void do_clean();
public:
baseClass():cle an_flag(false){ };
bool cleanup(){//return true unless cleaned before;
if(clean_flag) return false;
do_clean();
return clean_flag=true ;//set flag ,return done(true)!
};
void do_sth(){
//do some dirty thing:
...
clean_flag=fals e;//reset flag
};
virtual ~baseClass(){cl eanup()};
};

and you need overload 'do_clean' as well as the destructor in
subclasses:

class derivedClass:
public baseClass
{protected:
virtual void do_clean();
public:
virtual ~derivedClass() {cleanup()};
};

Jul 11 '07 #3
On Jul 11, 12:18 pm, terminator <farid.mehr...@ gmail.comwrote:
class baseClass{
protected:
bool clean_flag;
virtual void do_clean();
public:
baseClass():cle an_flag(false){ };
bool cleanup(){//return true unless cleaned before;
if(clean_flag) return false;
do_clean();
return clean_flag=true ;//set flag ,return done(true)!
};
void do_sth(){
//do some dirty thing:
...
clean_flag=fals e;//reset flag
};
virtual ~baseClass(){cl eanup()};

};
sorry,this one is better:
class baseClass{
private:
bool clean_flag;
//LOOK HERE:
bool set_clean(){
if (is_clean()) return false;
return clean_flage=tru e;
};
protected:
//LOOk,I AM HERE:
bool reset_clean(){
if(!is_clean()) return false;
clean_flage=fal se;
return true;
};
virtual void do_clean();
public:
baseClass():cle an_flag(false){ };
//WATCH THIS:
bool is_clean(){retu rn clean_flage;};
bool cleanup(){//return true unless cleaned before;
if(is_clean()) return false;
do_clean();
return set_clean();//set flag ,return done(true)!
};
void do_sth(){
//do some dirty thing:
...
//LOOK @ ME
reset_clean();//reset flag
};
virtual ~baseClass(){cl eanup()};
};

regards,
FM.

Jul 11 '07 #4

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

Similar topics

11
4351
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining it inline. Like this.
26
3972
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i add a virtual destructor like this : " virtual ~vechile". I get this error: Undefined first referenced symbol in file vtable for vechile /var/tmp//ccC9yD6Z.o
7
2018
by: dc | last post by:
Can anybody think of a situation where virtual function's address resolution/ something related to virtual is done at compile time
3
1912
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
3131
by: Alden Pierre | last post by:
Hello, http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7 As per the link above it's wise to have a virtual deconstructor when creating an abstract class. Here is when I'm little confused. Am I deleting the right object when I call the virtual deconstructor? I was under impression when creating a class if I'm not specifically allocating memory, do not implement deconstructor and let the default take care of it...
7
3083
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see item 7 for why this is virtual ...
17
3526
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
8
2327
by: Shraddha | last post by:
What is the use of "PURE vitual distructors"? And why we can not have vitual constructors?
8
2996
by: siddhu | last post by:
Dear experts, A virtual function has to have an address. So if an inline virtual function is actually inlined then in that case what does address of this function signify? How does compiler know at compile time about the actual object a pointer points to so that it can paste the correct inline function in the place of function call if the function is getting inlined?
0
8469
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
8903
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
8814
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...
1
8592
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8661
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...
0
7419
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6213
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
5684
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2800
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

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.