473,406 Members | 2,849 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,406 software developers and data experts.

virtual destructor problem

Hi all,

I've hit the wall with this "fairly" simple problem. As you can see in
the code below, my destructors don't do their job as they are supposed
to (I left them empty for this example). I'm running the Visual Leak
Detector library (on VS 2005) to detect memory leaks and whatever I've
tried it still complains..

Please can someone help me clean up properly?
--------

#include <iostream>
using namespace std;

class Base {
public:
Base(int baseInt) { m_baseInt=baseInt; }
virtual ~Base() {}
virtual void behave() const { cout << "I'm Base." << endl; }
virtual void prnt() const { cout << m_baseInt << endl; }
int m_baseInt;

};
class Derived : public Base {
public:
Derived(int baseInt, int derivedInt): Base(baseInt)
{ m_derivedInt=derivedInt; }
~Derived() {}
void behave() const { cout << "I'm Derived!" << endl; }
void prnt() const { cout << m_baseInt << " : " << m_derivedInt <<
endl; }
private:
int m_derivedInt;

};
int main() {
Base *anBase[2];
anBase[0] = new Base(11);
anBase[1] = new Derived(100, 1907);

for(int i = 0; i < 2; i++) {
anBase[i]->behave();
anBase[i]->prnt();
}
return 0;
}
Dec 9 '07 #1
8 1805
On Dec 9, 5:16 pm, lmf...@yahoo.com wrote:
Hi all,

I've hit the wall with this "fairly" simple problem. As you can see in
the code below, my destructors don't do their job as they are supposed
to (I left them empty for this example). I'm running the Visual Leak
Detector library (on VS 2005) to detect memory leaks and whatever I've
tried it still complains..

Please can someone help me clean up properly?
--------

#include <iostream>
using namespace std;

class Base {
public:
Base(int baseInt) { m_baseInt=baseInt; }
virtual ~Base() {}
virtual void behave() const { cout << "I'm Base." << endl; }
virtual void prnt() const { cout << m_baseInt << endl; }
int m_baseInt;

};

class Derived : public Base {
public:
Derived(int baseInt, int derivedInt): Base(baseInt)
{ m_derivedInt=derivedInt; }
~Derived() {}
void behave() const { cout << "I'm Derived!" << endl; }
void prnt() const { cout << m_baseInt << " : " << m_derivedInt <<
endl; }
private:
int m_derivedInt;

};

int main() {
Base *anBase[2];
anBase[0] = new Base(11);
anBase[1] = new Derived(100, 1907);

for(int i = 0; i < 2; i++) {
anBase[i]->behave();
anBase[i]->prnt();
}
return 0;

}
As your derived class as well as base class destructor's aren't doing
anything, there is no need for them to be declared as virtual.
Dec 9 '07 #2
On Dec 9, 5:34 pm, Rahul <sam_...@yahoo.co.inwrote:
On Dec 9, 5:16 pm, lmf...@yahoo.com wrote:


Hi all,
I've hit the wall with this "fairly" simple problem. As you can see in
the code below, my destructors don't do their job as they are supposed
to (I left them empty for this example). I'm running the Visual Leak
Detector library (on VS 2005) to detect memory leaks and whatever I've
tried it still complains..
Please can someone help me clean up properly?
--------
#include <iostream>
using namespace std;
class Base {
public:
Base(int baseInt) { m_baseInt=baseInt; }
virtual ~Base() {}
virtual void behave() const { cout << "I'm Base." << endl; }
virtual void prnt() const { cout << m_baseInt << endl; }
int m_baseInt;
};
class Derived : public Base {
public:
Derived(int baseInt, int derivedInt): Base(baseInt)
{ m_derivedInt=derivedInt; }
~Derived() {}
void behave() const { cout << "I'm Derived!" << endl; }
void prnt() const { cout << m_baseInt << " : " << m_derivedInt <<
endl; }
private:
int m_derivedInt;
};
int main() {
Base *anBase[2];
anBase[0] = new Base(11);
anBase[1] = new Derived(100, 1907);
for(int i = 0; i < 2; i++) {
anBase[i]->behave();
anBase[i]->prnt();
}
return 0;
}

As your derived class as well as base class destructor's aren't doing
anything, there is no need for them to be declared as virtual.- Hide quoted text -
There is "need" for the base destructor to be declared virtual because
otherwise the following will not work:

delete anBase[0];
delete anBase[1];

Dec 9 '07 #3
On Dec 9, 5:47 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
On Dec 9, 5:34 pm, Rahul <sam_...@yahoo.co.inwrote:
On Dec 9, 5:16 pm, lmf...@yahoo.com wrote:
Hi all,
I've hit the wall with this "fairly" simple problem. As you can see in
the code below, my destructors don't do their job as they are supposed
to (I left them empty for this example). I'm running the Visual Leak
Detector library (on VS 2005) to detect memory leaks and whatever I've
tried it still complains..
Please can someone help me clean up properly?
--------
#include <iostream>
using namespace std;
class Base {
public:
Base(int baseInt) { m_baseInt=baseInt; }
virtual ~Base() {}
virtual void behave() const { cout << "I'm Base." << endl; }
virtual void prnt() const { cout << m_baseInt << endl; }
int m_baseInt;
};
class Derived : public Base {
public:
Derived(int baseInt, int derivedInt): Base(baseInt)
{ m_derivedInt=derivedInt; }
~Derived() {}
void behave() const { cout << "I'm Derived!" << endl; }
void prnt() const { cout << m_baseInt << " : " << m_derivedInt <<
endl; }
private:
int m_derivedInt;
};
int main() {
Base *anBase[2];
anBase[0] = new Base(11);
anBase[1] = new Derived(100, 1907);
for(int i = 0; i < 2; i++) {
anBase[i]->behave();
anBase[i]->prnt();
}
return 0;
}
As your derived class as well as base class destructor's aren't doing
anything, there is no need for them to be declared as virtual.- Hide quoted text -

There is "need" for the base destructor to be declared virtual because
otherwise the following will not work:

delete anBase[0];
delete anBase[1];
In the sense, it just wouldn't invoke the derived class destructor and
as the derived class destructor is not doing anything.
I don't see any negative impact. Yes i'm not saying it is a thumb
rule...
Dec 9 '07 #4
On Dec 9, 5:52 pm, Rahul <sam_...@yahoo.co.inwrote:
On Dec 9, 5:47 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:


On Dec 9, 5:34 pm, Rahul <sam_...@yahoo.co.inwrote:
On Dec 9, 5:16 pm, lmf...@yahoo.com wrote:
Hi all,
I've hit the wall with this "fairly" simple problem. As you can see in
the code below, my destructors don't do their job as they are supposed
to (I left them empty for this example). I'm running the Visual Leak
Detector library (on VS 2005) to detect memory leaks and whatever I've
tried it still complains..
Please can someone help me clean up properly?
--------
#include <iostream>
using namespace std;
class Base {
public:
Base(int baseInt) { m_baseInt=baseInt; }
virtual ~Base() {}
virtual void behave() const { cout << "I'm Base." << endl; }
virtual void prnt() const { cout << m_baseInt << endl; }
int m_baseInt;
};
class Derived : public Base {
public:
Derived(int baseInt, int derivedInt): Base(baseInt)
{ m_derivedInt=derivedInt; }
~Derived() {}
void behave() const { cout << "I'm Derived!" << endl; }
void prnt() const { cout << m_baseInt << " : " << m_derivedInt <<
endl; }
private:
int m_derivedInt;
};
int main() {
Base *anBase[2];
anBase[0] = new Base(11);
anBase[1] = new Derived(100, 1907);
for(int i = 0; i < 2; i++) {
anBase[i]->behave();
anBase[i]->prnt();
}
return 0;
}
As your derived class as well as base class destructor's aren't doing
anything, there is no need for them to be declared as virtual.- Hide quoted text -
There is "need" for the base destructor to be declared virtual because
otherwise the following will not work:
delete anBase[0];
delete anBase[1];

In the sense, it just wouldn't invoke the derived class destructor and
as the derived class destructor is not doing anything.
I don't see any negative impact. Yes i'm not saying it is a thumb
rule...

The negative impact is that it is undefined behaviour as per the
standards. What bad will happen depends on the particular
implementation. May cause a memory leak. I cannot comprehend what else
can go wrong in cases of undefined behaviours.
Dec 9 '07 #5
On Dec 9, 6:02 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
On Dec 9, 5:52 pm, Rahul <sam_...@yahoo.co.inwrote:
On Dec 9, 5:47 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
On Dec 9, 5:34 pm, Rahul <sam_...@yahoo.co.inwrote:
On Dec 9, 5:16 pm, lmf...@yahoo.com wrote:
Hi all,
I've hit the wall with this "fairly" simple problem. As you can see in
the code below, my destructors don't do their job as they are supposed
to (I left them empty for this example). I'm running the Visual Leak
Detector library (on VS 2005) to detect memory leaks and whatever I've
tried it still complains..
Please can someone help me clean up properly?
--------
#include <iostream>
using namespace std;
class Base {
public:
Base(int baseInt) { m_baseInt=baseInt; }
virtual ~Base() {}
virtual void behave() const { cout << "I'm Base." << endl; }
virtual void prnt() const { cout << m_baseInt << endl; }
int m_baseInt;
};
class Derived : public Base {
public:
Derived(int baseInt, int derivedInt): Base(baseInt)
{ m_derivedInt=derivedInt; }
~Derived() {}
void behave() const { cout << "I'm Derived!" << endl; }
void prnt() const { cout << m_baseInt << " : " << m_derivedInt <<
endl; }
private:
int m_derivedInt;
};
int main() {
Base *anBase[2];
anBase[0] = new Base(11);
anBase[1] = new Derived(100, 1907);
for(int i = 0; i < 2; i++) {
anBase[i]->behave();
anBase[i]->prnt();
}
return 0;
}
As your derived class as well as base class destructor's aren't doing
anything, there is no need for them to be declared as virtual.- Hide quoted text -
There is "need" for the base destructor to be declared virtual because
otherwise the following will not work:
delete anBase[0];
delete anBase[1];
In the sense, it just wouldn't invoke the derived class destructor and
as the derived class destructor is not doing anything.
I don't see any negative impact. Yes i'm not saying it is a thumb
rule...

The negative impact is that it is undefined behaviour as per the
standards. What bad will happen depends on the particular
implementation. May cause a memory leak. I cannot comprehend what else
can go wrong in cases of undefined behaviours.
Oh, if that is the case, why does't the standard mandate dest to be
always virtual when some other function is also virtual in the base
class? It would be easy for the compiler to spot out the error too,
isn't it?
Dec 9 '07 #6
Rahul wrote:
:: On Dec 9, 6:02 pm, Abhishek Padmanabh
:: <abhishek.padman...@gmail.comwrote:
::: On Dec 9, 5:52 pm, Rahul <sam_...@yahoo.co.inwrote:
:::
:::
:::
:::: On Dec 9, 5:47 pm, Abhishek Padmanabh
:::: <abhishek.padman...@gmail.comwrote:
:::
::::: On Dec 9, 5:34 pm, Rahul <sam_...@yahoo.co.inwrote:
:::
:::::: On Dec 9, 5:16 pm, lmf...@yahoo.com wrote:
:::
::::::: Hi all,
:::
::::::: I've hit the wall with this "fairly" simple problem. As you
::::::: can see in the code below, my destructors don't do their job
::::::: as they are supposed to (I left them empty for this example).
::::::: I'm running the Visual Leak Detector library (on VS 2005) to
::::::: detect memory leaks and whatever I've tried it still
::::::: complains..
:::
::::::: Please can someone help me clean up properly?
::::::: --------
:::
::::::: #include <iostream>
::::::: using namespace std;
:::
::::::: class Base {
::::::: public:
::::::: Base(int baseInt) { m_baseInt=baseInt; }
::::::: virtual ~Base() {}
::::::: virtual void behave() const { cout << "I'm Base." <<
::::::: endl; } virtual void prnt() const { cout << m_baseInt <<
::::::: endl; } int m_baseInt;
:::
::::::: };
:::
::::::: class Derived : public Base {
::::::: public:
::::::: Derived(int baseInt, int derivedInt): Base(baseInt)
::::::: { m_derivedInt=derivedInt; }
::::::: ~Derived() {}
::::::: void behave() const { cout << "I'm Derived!" << endl; }
::::::: void prnt() const { cout << m_baseInt << " : " <<
::::::: m_derivedInt << endl; }
::::::: private:
::::::: int m_derivedInt;
:::
::::::: };
:::
::::::: int main() {
::::::: Base *anBase[2];
::::::: anBase[0] = new Base(11);
::::::: anBase[1] = new Derived(100, 1907);
:::
::::::: for(int i = 0; i < 2; i++) {
::::::: anBase[i]->behave();
::::::: anBase[i]->prnt();
::::::: }
::::::: return 0;
:::
::::::: }
:::
:::::: As your derived class as well as base class destructor's
:::::: aren't doing
:::::: anything, there is no need for them to be declared as
:::::: virtual.- Hide quoted text -
:::
::::: There is "need" for the base destructor to be declared virtual
::::: because otherwise the following will not work:
:::
::::: delete anBase[0];
::::: delete anBase[1];
:::
:::: In the sense, it just wouldn't invoke the derived class
:::: destructor and as the derived class destructor is not doing
:::: anything.
:::: I don't see any negative impact. Yes i'm not saying it is a thumb
:::: rule...
:::
::: The negative impact is that it is undefined behaviour as per the
::: standards. What bad will happen depends on the particular
::: implementation. May cause a memory leak. I cannot comprehend what
::: else can go wrong in cases of undefined behaviours.
::
:: Oh, if that is the case, why does't the standard mandate dest to be
:: always virtual when some other function is also virtual in the base
:: class? It would be easy for the compiler to spot out the error too,
:: isn't it?

But it is only an error if you do a delete through a pointer to base.
If you just have a local Derived variable, or use a pointer to
Derived, it is ok.
Bo Persson
Dec 9 '07 #7
On Dec 9, 7:52 am, Rahul <sam_...@yahoo.co.inwrote:
On Dec 9, 5:47 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
On Dec 9, 5:34 pm, Rahul <sam_...@yahoo.co.inwrote:
On Dec 9, 5:16 pm, lmf...@yahoo.com wrote:
Hi all,
I've hit the wall with this "fairly" simple problem. As you can see in
the code below, my destructors don't do their job as they are supposed
to (I left them empty for this example). I'm running the Visual Leak
Detector library (on VS 2005) to detect memory leaks and whatever I've
tried it still complains..
Please can someone help me clean up properly?
--------
#include <iostream>
using namespace std;
class Base {
public:
Base(int baseInt) { m_baseInt=baseInt; }
virtual ~Base() {}
virtual void behave() const { cout << "I'm Base." << endl; }
virtual void prnt() const { cout << m_baseInt << endl; }
int m_baseInt;
};
class Derived : public Base {
public:
Derived(int baseInt, int derivedInt): Base(baseInt)
{ m_derivedInt=derivedInt; }
~Derived() {}
void behave() const { cout << "I'm Derived!" << endl; }
void prnt() const { cout << m_baseInt << " : " << m_derivedInt <<
endl; }
private:
int m_derivedInt;
};
int main() {
Base *anBase[2];
anBase[0] = new Base(11);
anBase[1] = new Derived(100, 1907);
for(int i = 0; i < 2; i++) {
anBase[i]->behave();
anBase[i]->prnt();
}
return 0;
}
As your derived class as well as base class destructor's aren't doing
anything, there is no need for them to be declared as virtual.- Hide quoted text -
There is "need" for the base destructor to be declared virtual because
otherwise the following will not work:
delete anBase[0];
delete anBase[1];

In the sense, it just wouldn't invoke the derived class destructor and
as the derived class destructor is not doing anything.
I don't see any negative impact. Yes i'm not saying it is a thumb
rule...
If you allocate and store objects using a pointer to a base class,
the standard requires the base class destructor to be virtual.

Don't get fooled by an empty d~tor body, they may now (or later in
your developmental process) invoke other destructors (ie: member
d~tors). In this scenario you have integer members so those d~tors are
definitely doing something.
Dec 9 '07 #8
On 2007-12-09 16:48:00 -0500, Salt_Peter <pj*****@yahoo.comsaid:
>
If you allocate and store objects using a pointer to a base class,
the standard requires the base class destructor to be virtual.
No, it doesn't. The relevant requirement is that if you delete an
object of a derived type through a pointer to one of its base types and
that base type does not have a virtual destructor, the behavior of the
program is undefined. That is, a virtual destructor is required if you
delete the object through a pointer to base. Not if you only allocate
and store it.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Dec 10 '07 #9

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

Similar topics

23
by: heted7 | last post by:
Hi, Most of the books on C++ say something like this: "A virtual destructor should be defined if the class contains at least one virtual member function." My question is: why is it only for...
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...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
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...
3
by: Pravesh | last post by:
Hello All, I had some query regarding virtual functions/destructors. If a class is having some/all of its methods that are virtual then is it recommended that it should also have virtual...
5
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
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;"...
3
by: Juha Nieminen | last post by:
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...
3
by: GAURAV AGRAWAL | last post by:
Hi Guys, Can someone please explain me why this is happening #include<iostream> using namespace std; class a { public: int a1; // If I remove this it'll work fine
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...

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.