Destructor thru parent class

S

Sashi

class parent{

Parent(){};
~Parent(){};
}

Child: public Parent{

Child(){};
~Child(){};
}


void destroy( Parent *ptr)
{delete ptr;}

int main(int argc, char** argv){

Child c_ptr = new Child;
Parent p_ptr = new Parent;

destroy( c_ptr);
destroy(p_ptr);

exit 0;
}

I think this question must have been discussed to death but here it
goes again.

What'll happen above? Nothing catastrophic, as there is no dynamically
allocated memory. Am I right?

Also, what is the more general problem of doing such a delete? i.e.
deleting a child object pointed to by a parent pointer?
Thanks,
Sashi
 
M

mlimber

Sashi said:
class parent{

Parent(){};
~Parent(){};
}

Child: public Parent{

Child(){};
~Child(){};
}


void destroy( Parent *ptr)
{delete ptr;}

int main(int argc, char** argv){

Child c_ptr = new Child;
Parent p_ptr = new Parent;

destroy( c_ptr);
destroy(p_ptr);

exit 0;
}

I think this question must have been discussed to death but here it
goes again.

Indeed, that's why it's in the FAQ.
What'll happen above? Nothing catastrophic, as there is no dynamically
allocated memory. Am I right?

Also, what is the more general problem of doing such a delete? i.e.
deleting a child object pointed to by a parent pointer?

http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7

Cheers! --M
 
D

dasjotre

Sashi said:
class parent{ class Parent {

constructor and destructor are private
make them public
Parent(){};
~Parent(){};

you don't need semicolons here

you need one here
Child: public Parent{ class Child: public Parent{

same as above, use public
Child(){};
~Child(){};
semicolons!


semicolon!


void destroy( Parent *ptr)
{delete ptr;}

int main(int argc, char** argv){

Child c_ptr = new Child;
Parent p_ptr = new Parent;

Child * c_ptr = new Child;
Parent * p_ptr = new Parent;
destroy( c_ptr);
destroy(p_ptr);

exit 0;

return 0;
}

I think this question must have been discussed to death but here it
goes again.

What'll happen above? Nothing catastrophic, as there is no dynamically
allocated memory. Am I right?

new dynamically allocates memory and calls object's constructor.
there are dynamically allocated Child and Parent objects

add

std::cout << "~Parent\n";

to Parent's destructor and

std::cout << "~Child\n";

to Child's destructor and try it. then declare Parent's destructor
virtual

virtual ~Parent()

and try again.
 
S

Sashi

dasjotre said:
constructor and destructor are private
make them public


you don't need semicolons here


you need one here


same as above, use public


Child * c_ptr = new Child;
Parent * p_ptr = new Parent;


return 0;


new dynamically allocates memory and calls object's constructor.
there are dynamically allocated Child and Parent objects

add

std::cout << "~Parent\n";

to Parent's destructor and

std::cout << "~Child\n";

to Child's destructor and try it. then declare Parent's destructor
virtual

virtual ~Parent()

and try again.

Ah, yes, apologies for the imperfect syntax. It's more like pseudocode
to illustrate a point.

My question simply boils down to this: what will happen when you call a
delete on the parent class pointer that actually points to a child
object?
I was asked this in an interview yesterday. I used to think I know c++
reasonably well and my inability to answer this question caused me
quite a bit of embarrassment.
The answer: "undefined behavior"
-Sashi
 
D

dasjotre

Sashi said:
Ah, yes, apologies for the imperfect syntax. It's more like pseudocode
to illustrate a point.

My question simply boils down to this: what will happen when you call a
delete on the parent class pointer that actually points to a child
object?
I was asked this in an interview yesterday. I used to think I know c++
reasonably well and my inability to answer this question caused me
quite a bit of embarrassment.
The answer: "undefined behavior"

The question is whether you want it this way or not.
 
M

mlimber

Sashi said:
My question simply boils down to this: what will happen when you call a
delete on the parent class pointer that actually points to a child
object?
I was asked this in an interview yesterday. I used to think I know c++
reasonably well and my inability to answer this question caused me
quite a bit of embarrassment.
The answer: "undefined behavior"

Or what the FAQ I cited calls "Yuck."

Cheers! --M
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top