Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Derived Destructors

Reply
Thread Tools

Derived Destructors

 
 
Bushido Hacks
Guest
Posts: n/a
 
      04-15-2007
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 multidimmensional 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::~Derived(){
// Should these lines be here?
for(int i = 0; i < m; i++){delete[] p[i];};
delete[] p;
};

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-15-2007
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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-15-2007
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


 
Reply With Quote
 
simonod
Guest
Posts: n/a
 
      04-15-2007
On Apr 15, 6:41 pm, "Bushido Hacks" <(E-Mail Removed)> wrote:
> 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 multidimmensional 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::~Derived(){
> // 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

 
Reply With Quote
 
Bushido Hacks
Guest
Posts: n/a
 
      04-15-2007
On Apr 15, 2:01 pm, "simonod" <(E-Mail Removed)> wrote:
> On Apr 15, 6:41 pm, "Bushido Hacks" <(E-Mail Removed)> wrote:
>
>
>
> > 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 multidimmensional 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::~Derived(){
> > // 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?

 
Reply With Quote
 
Zeppe
Guest
Posts: n/a
 
      04-16-2007
Bushido Hacks wrote:
> On Apr 15, 2:01 pm, "simonod" <(E-Mail Removed)> wrote:
>> On Apr 15, 6:41 pm, "Bushido Hacks" <(E-Mail Removed)> wrote:
>>
>>
>>
>>> 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 multidimmensional 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::~Derived(){
>>> // 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) developereo@hotmail.com C++ 4 05-23-2007 09:32 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
If declared as virtual in base, its derived version also is virtual. Why not for destructors? qazmlp C++ 7 07-27-2004 03:10 PM
Destructors of Derived classes Mike C++ 1 07-30-2003 03:52 PM



Advertisments