473,320 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Pointer to classes when deleted

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;
}

Jul 22 '05 #1
3 1331
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

Jul 22 '05 #2
"Steven Green" <st************@verizon.net> wrote...
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
Jul 22 '05 #3
Ok I have been in Javaland too long, my brain frosted over and I
forgot about using virtual.

Boy are my cheeks red.

On Fri, 26 Dec 2003 02:13:25 +0000, Victor Bazarov wrote:
"Steven Green" <st************@verizon.net> wrote...
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


Jul 22 '05 #4

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

Similar topics

2
by: Asfand Yar Qazi | last post by:
Hello. Partly for learning purposes, I have written a smart pointer class. Could you please tell me what's wrong with it? (I know there's something wrong with it, but just not what!) Note...
4
by: Venn Syii | last post by:
I've searched all the forums but cannot find an answer to this question. I do the following: vector<MyClass*> myClassList; Later in the program I try to add to myClassList with a...
7
by: andylcx | last post by:
Hi all, I have a question about the code below. Any idea abuot that, thanks in advance! myclass.h class myclass { public: myclass(); ~myclass(); void setdata();
17
by: Divick | last post by:
Hi, I am designing an API and the problem that I have is more of a design issue. In my API say I have a class A and B, as shown below class A{ public: void doSomethingWithB( B * b) { //do...
7
by: shanemh | last post by:
I'm starting out with c++ and for some reason I cant get my brain around this one: If I have the following: void Foo (someClass& x) {} int Main (void) {
7
by: jc | last post by:
i'm developing a project using vc++. the main exe is a win32 application. it needs two dlls. one is my own implementation of string operations. the other dll is to parse a2l files(it is similar to...
30
by: Jess | last post by:
Hello, I tried a program as follows: include<iostream> using namespace std; class A{ public:
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
5
by: Soumen | last post by:
Hi, I've requirement to observe a raw pointer (i.e. I should be able to query if the pointer I'm using is still valid or not but when the observer goes out of scope, the resource -- memory --...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.