Pointer to classes when deleted

S

Steven Green

I came up with this question while reviewing another message in
this newsgroup. I've been away on Java for quite a bit and have
a question on deletions.

I think I already know the answer but I thought I would check
with the group to be sure.

Why does C++ not call delete for both classes when the pointer
is released even when what was allocated was the derived class?

It seems to make pointers even more dangerous than I remembered.

#include <iostream>
using namespace std;
class base
{
public:
~base() {cout << "base" << endl;}
};

class derived: public base
{
public:
~derived() {cout << "derived" << endl;}
};

int main()
{
base *r = new derived();
delete r;
}
 
J

Jeff Schwab

Why does C++ not call delete for both classes when the pointer
is released even when what was allocated was the derived class?

It does, assuming you declared the base-class destructor to be virtual.

If you did *not* make the base-class destructor virtual, then the
run-time environment would have to find a place to store the "real" type
of the object along with the pointer. That would require overhead
unacceptable to the designers of the language (and I am grateful to them
for their stinginess). Java's willing to trade a lot more overhead to
protect you from yourself, and has a far more extensive run-time
environment, anyway. The reason is that Oak originally was meant to run
on embedded systems, where leaks were absolutely unacceptable. If the
program crashed, some consumer device would stop working.

Hth,
Jeff
 
V

Victor Bazarov

Steven Green said:
I came up with this question while reviewing another message in
this newsgroup. I've been away on Java for quite a bit and have
a question on deletions.

I think I already know the answer but I thought I would check
with the group to be sure.

Why does C++ not call delete for both classes when the pointer
is released even when what was allocated was the derived class?

It does if the destructor is virtual. If the destructor is not
virtual deleting an object of the derived class through a pointer
to a base class causes undefined behaviour. For all we care, your
compiler might just do what you think it should.
It seems to make pointers even more dangerous than I remembered.

Yes, no doubt. And it makes the resulting code faster, since it
doesn't have to (has no responsibility of) keep track of these
things.
#include <iostream>
using namespace std;
class base
{
public:
~base() {cout << "base" << endl;}
};

class derived: public base
{
public:
~derived() {cout << "derived" << endl;}
};

int main()
{
base *r = new derived();
delete r;

Undefined behaviour occurs.

Victor
 
S

Steven Green

Ok I have been in Javaland too long, my brain frosted over and I
forgot about using virtual.

Boy are my cheeks red.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top