What should a virtual destructor contain?

F

Fletcher Glenn

I've done some web searching and I cannot find a suitable answer to this
question for the following circumstances. Consider:

class Base
{
private:
Otherclass* instance;
public:
Base() { instance = new Otherclass; }
virtual ~Base() { delete instance; }
};

class Derived : public Base
{
private:
Someclass *xxx;
public:
Derived() { xxx = new Someclass; }
virtual ~Derived() {
// The following is incomplete destruction for class Base
// What should be here?
delete xxx;
}
};

Thanks in advance,
 
N

Noah Roberts

Fletcher said:
I've done some web searching and I cannot find a suitable answer to this
question for the following circumstances. Consider:

class Base
{
private:
Otherclass* instance;
public:
Base() { instance = new Otherclass; }
virtual ~Base() { delete instance; }
};

class Derived : public Base
{
private:
Someclass *xxx;
public:
Derived() { xxx = new Someclass; }
virtual ~Derived() {
// The following is incomplete destruction for class Base
// What should be here?
delete xxx;
}
};

What you have appears adiquate for the situation. I don't understand
the comments but the code itself is ok...minus the general bad plan of
using pointers in that manner.
 
F

Fletcher Glenn

Noah Roberts said:
What you have appears adiquate for the situation. I don't understand
the comments but the code itself is ok...minus the general bad plan of
using pointers in that manner.

It was my understanding that making the destructor virtual would result in
only the top level destructor being called, therefore leaking
Base::instance.
Am I wrong?
 
I

Ian Collins

Fletcher said:
I've done some web searching and I cannot find a suitable answer to this
question for the following circumstances. Consider:

class Base
{
private:
Otherclass* instance;
public:
Base() { instance = new Otherclass; }
virtual ~Base() { delete instance; }
};

class Derived : public Base
{
private:
Someclass *xxx;
public:
Derived() { xxx = new Someclass; }
virtual ~Derived() {
Superfluous 'virtual'.
// The following is incomplete destruction for class Base
// What should be here?
Why? Base deletes instance, Derived deletes xxx.

If you used std::auto_ptr, there wouldn't be a problem!
 
J

JustBoo

Superfluous 'virtual'.

Concerning the comment above: Like so many things in programming,
someone will pronounce something they personally don't like as an
absolute, when in fact, it is not.

Using the virtual keyword in derived classes is technically redundant,
but in anything other than *trivial* example programs they can and are
used as a form of documentation. I tire quickly of searching through a
large class hierarchy looking to make sure if a function is virtual or
not. (Personally, I make flat hierarchies using containment and
composition, but I'm not always in control of the classes I have to
use.)

I did an informal study a while back and concluded there is absolutely
no performance penalty, or any kind of negative associated with
putting the virtual keyword in derived classes. Quite the contrary, it
lets the programmer instantly know what he is working with.

However beautiful the strategy, you should occasionally
look at the results. - Winston Churchill
 
P

peter koch

Fletcher Glenn skrev:
I've done some web searching and I cannot find a suitable answer to this
question for the following circumstances. Consider:

class Base
{
private:
Otherclass* instance;
public:
Base() { instance = new Otherclass; }
virtual ~Base() { delete instance; }
};

class Derived : public Base
{
private:
Someclass *xxx;
public:
Derived() { xxx = new Someclass; }
virtual ~Derived() {
// The following is incomplete destruction for class Base
// What should be here?
delete xxx;
}
};
First, I do not see why instance and xxx are pointers. The way you use
them, they might just as well be value objects.
Secondly your comment in Deriveds destructor is wrong. The base class
is not incompletely destructed.
Last, the (only) reason for a virtual destructor is to enable you to
delete a child-class via a pointer to base. There is not any specific
reason to do anything special there. So in your case, I would just
replace the pointers with value objects and keep all destructors empty.

/Peter
 
I

Ian Collins

JustBoo said:
Concerning the comment above: Like so many things in programming,
someone will pronounce something they personally don't like as an
absolute, when in fact, it is not.

Using the virtual keyword in derived classes is technically redundant,
but in anything other than *trivial* example programs they can and are
used as a form of documentation. I tire quickly of searching through a
large class hierarchy looking to make sure if a function is virtual or
not. (Personally, I make flat hierarchies using containment and
composition, but I'm not always in control of the classes I have to
use.)
So if someone has introduced a virtual destructor somewhere other than
the original base class, would you spot this?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top