By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,996 Members | 1,179 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,996 IT Pros & Developers. It's quick & easy.

Destructor thru parent class

P: n/a
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

Nov 30 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Sashi wrote:
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-lit....html#faq-20.7

Cheers! --M

Nov 30 '06 #2

P: n/a
Sashi wrote:
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.

Nov 30 '06 #3

P: n/a

dasjotre wrote:
Sashi wrote:
class Parent {
class Child: public Parent{
The names you use possibly reveal some misunderstanding of inheritance
on your side. Considering that in your example Child is a kind of a
Parent, which doesn't seem right. In real life all parents are
somebody's children and some children are also parents.

Check http://www.parashift.com/c++-faq-lit...heritance.html

Nov 30 '06 #4

P: n/a

dasjotre wrote:
Sashi wrote:
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.
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

Nov 30 '06 #5

P: n/a

Sashi wrote:
dasjotre wrote:
Sashi wrote:
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.

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.

Nov 30 '06 #6

P: n/a
Sashi wrote:
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

Nov 30 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.