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

Home Posts Topics Members FAQ

Derived Destructors

I think this message got lost the first time I sent it, so I'm
reposting it.
I need a little help with Destructors.
Suppose I created a multidimmension al array in a Base class that was
also accessable in a Derived class, would I need to delete the array
in the Derived class if I declared my destructors as virtual?

class Base
{
public:
...
virtual ~Base();
protected:
int m; // rows
int n; // cols
double** p;
};

class Derived : public Base
{
public:
...
virtual ~Derived();
};

Base::~Base()
{
for(int i = 0; i < m; i++){delete[] p[i];};
delete[] p;
};

Derived::~Deriv ed(){
// Should these lines be here?
for(int i = 0; i < m; i++){delete[] p[i];};
delete[] p;
};

Apr 15 '07 #1
5 1817
Bushido Hacks wrote:
I think this message got lost the first time I sent it, so I'm
reposting it.
It didn't. I replied. The answer is 'now'.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 15 '07 #2
Bushido Hacks wrote:
I think this message got lost the first time I sent it, so I'm
reposting it.
It didn't. I replied. The answer is "no, it shouldn't".
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 15 '07 #3
On Apr 15, 6:41 pm, "Bushido Hacks" <bushidoha...@g mail.comwrote:
I think this message got lost the first time I sent it, so I'm
reposting it.
I need a little help with Destructors.
Suppose I created a multidimmension al array in a Base class that was
also accessable in a Derived class, would I need to delete the array
in the Derived class if I declared my destructors as virtual?

class Base
{
public:
...
virtual ~Base();
protected:
int m; // rows
int n; // cols
double** p;

};

class Derived : public Base
{
public:
...
virtual ~Derived();

};

Base::~Base()
{
for(int i = 0; i < m; i++){delete[] p[i];};
delete[] p;

};

Derived::~Deriv ed(){
// Should these lines be here?
for(int i = 0; i < m; i++){delete[] p[i];};
delete[] p;

};- Hide quoted text -

- Show quoted text -
Hi,

For me, the quick answer is you always delete where you new. In your
case, if you new'd the array in the base class then you also delete it
there (and nowhere else). The fact that the destructor is virtual
makes no difference to this fact: what that does is to make sure that
delete calls the correct destructor of an object even if you only have
a pointer to a base class of that object. Correctly calling a derived
class destructor will still mean that the base class destructor gets
called too, and you're memory will be free'd.

Hope that helps,
Si

Apr 15 '07 #4
On Apr 15, 2:01 pm, "simonod" <simonodw...@gm ail.comwrote:
On Apr 15, 6:41 pm, "Bushido Hacks" <bushidoha...@g mail.comwrote:
I think this message got lost the first time I sent it, so I'm
reposting it.
I need a little help with Destructors.
Suppose I created a multidimmension al array in a Base class that was
also accessable in a Derived class, would I need to delete the array
in the Derived class if I declared my destructors as virtual?
class Base
{
public:
...
virtual ~Base();
protected:
int m; // rows
int n; // cols
double** p;
};
class Derived : public Base
{
public:
...
virtual ~Derived();
};
Base::~Base()
{
for(int i = 0; i < m; i++){delete[] p[i];};
delete[] p;
};
Derived::~Deriv ed(){
// Should these lines be here?
for(int i = 0; i < m; i++){delete[] p[i];};
delete[] p;
};- Hide quoted text -
- Show quoted text -

Hi,

For me, the quick answer is you always delete where you new. In your
case, if you new'd the array in the base class then you also delete it
there (and nowhere else). The fact that the destructor is virtual
makes no difference to this fact: what that does is to make sure that
delete calls the correct destructor of an object even if you only have
a pointer to a base class of that object. Correctly calling a derived
class destructor will still mean that the base class destructor gets
called too, and you're memory will be free'd.

Hope that helps,
Si
What if the base class and the derived class both have a assignment
operator functions? If I use the delete and new functions in the
derived definition, do I still need those lines in Derived's
destructor?

Apr 15 '07 #5
Bushido Hacks wrote:
On Apr 15, 2:01 pm, "simonod" <simonodw...@gm ail.comwrote:
>On Apr 15, 6:41 pm, "Bushido Hacks" <bushidoha...@g mail.comwrote:
>>I think this message got lost the first time I sent it, so I'm
reposting it.
I need a little help with Destructors.
Suppose I created a multidimmension al array in a Base class that was
also accessable in a Derived class, would I need to delete the array
in the Derived class if I declared my destructors as virtual?
class Base
{
public:
...
virtual ~Base();
protected:
int m; // rows
int n; // cols
double** p;
};
class Derived : public Base
{
public:
...
virtual ~Derived();
};
Base::~Base ()
{
for(int i = 0; i < m; i++){delete[] p[i];};
delete[] p;
};
Derived::~Der ived(){
// Should these lines be here?
for(int i = 0; i < m; i++){delete[] p[i];};
delete[] p;
};- Hide quoted text -
- Show quoted text -
Hi,

For me, the quick answer is you always delete where you new. In your
case, if you new'd the array in the base class then you also delete it
there (and nowhere else). The fact that the destructor is virtual
makes no difference to this fact: what that does is to make sure that
delete calls the correct destructor of an object even if you only have
a pointer to a base class of that object. Correctly calling a derived
class destructor will still mean that the base class destructor gets
called too, and you're memory will be free'd.

Hope that helps,
Si

What if the base class and the derived class both have a assignment
operator functions? If I use the delete and new functions in the
derived definition, do I still need those lines in Derived's
destructor?
No, if you delete in the base, it's ok. What sigmonod was trying to tell
you is that if the array is defined in your base class, and is allocated
in your base class, no allocation or deallocation should be done in the
derived class. It's a matter of good programming, not of C++
constraints. You have to try to keep things separated, and to manage
objects in a single class. That is, if not REALLY needed, avoid
protected member variables.

Anyway, your original question actually made sense: the virtual
destructor has a somewhat particular behaviour, because usually when a
virtual method is invoked, only the derived one is actually executed.

Regards,

Zeppe
Apr 16 '07 #6

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

Similar topics

1
2031
by: Mike | last post by:
I seem to be having a problem with the way destructors are called with derived classes. Below is a short example of what I'm trying to do. In the example a is the base class. It has a function 'action' which destroys the object. As I understand derived classes and destructors, the destructor of the derived class should be called first, then the destructor of the base class. Really, for my purposes, order doesn't matter. In my example...
24
3296
by: Shao Zhang | last post by:
Hi, I am not sure if the virtual keyword for the derived classes are required given that the base class already declares it virtual. class A { public: virtual ~A();
7
1886
by: qazmlp | last post by:
When a member function is declared as virtual in the base class, the derived class versions of it are always treated as virtual. I am just wondering, why the same concept was not used for the destructors. What I am expecting is, if the destructor is declared as virtual in base, the destructors of all its derived classes also should be virtual always. What exactly is the reason for not having it so?
14
2197
by: jimmy | last post by:
Hi, Please forgive me if this is elementary. I can't seem to find the solution anywhere. // foo() is only declared/defined in Derived Base* base = new Derived(); ((Derived*)base)->foo(); Given that I would like to use a Base*; is casting the only solution?
6
5873
by: eselk | last post by:
If I have: class A { public: class some_base_class **Obj; }; And I would like to redefine "Obj" in a class derived from class A, something like this maybe:
2
2636
by: Jessica | last post by:
I have a base class and a derived class, but I am getting errors when I try to access functions of the derived class. Simplified version of my code is as follows: //////////////// // test2.hh class BaseClass {
6
2940
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by calling Mesh class functions. Let's say they point to each other like this: class Vertex { HalfEdge *edge; }; class HalfEdge { Vertex* vert;
9
2042
by: desktop | last post by:
On this page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html Shape specify the virtual function: virtual double Intersect( const Shape& s) = 0; then the derived class Circle also specify:
15
1942
by: iu2 | last post by:
Hi all, can someone help me with this? I'm trying to shorthen some logging function in our project, i.e., instead of LogString("...); use a more convenient log() << "String here " << 12.33 << " and a number";
0
8680
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
8609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9169
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
9030
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
8899
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
8871
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
6528
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
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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.