By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,688 Members | 1,107 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,688 IT Pros & Developers. It's quick & easy.

virtual destructor problem

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.