473,386 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1369
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():clean_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=false;//reset flag
};
virtual ~baseClass(){cleanup()};
};

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():clean_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=false;//reset flag
};
virtual ~baseClass(){cleanup()};

};
sorry,this one is better:
class baseClass{
private:
bool clean_flag;
//LOOK HERE:
bool set_clean(){
if (is_clean()) return false;
return clean_flage=true;
};
protected:
//LOOk,I AM HERE:
bool reset_clean(){
if(!is_clean()) return false;
clean_flage=false;
return true;
};
virtual void do_clean();
public:
baseClass():clean_flag(false){};
//WATCH THIS:
bool is_clean(){return 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(){cleanup()};
};

regards,
FM.

Jul 11 '07 #4

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

Similar topics

11
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...
26
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...
7
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
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...
6
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...
7
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...
17
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;"...
8
by: Shraddha | last post by:
What is the use of "PURE vitual distructors"? And why we can not have vitual constructors?
8
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.