Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Destructor thru parent class

Reply
Thread Tools

Destructor thru parent class

 
 
Sashi
Guest
Posts: n/a
 
      11-30-2006
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

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      11-30-2006
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

 
Reply With Quote
 
 
 
 
dasjotre
Guest
Posts: n/a
 
      11-30-2006
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.

 
Reply With Quote
 
dasjotre
Guest
Posts: n/a
 
      11-30-2006

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

 
Reply With Quote
 
Sashi
Guest
Posts: n/a
 
      11-30-2006

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

 
Reply With Quote
 
dasjotre
Guest
Posts: n/a
 
      11-30-2006

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.

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      11-30-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
newbie -- smart pointer destructor called without destructor everbeing called Jimmy Hartzell C++ 2 05-20-2008 02:20 AM
newbie -- smart pointer destructor called without destructor everbeing called Jimmy Hartzell C++ 0 05-19-2008 07:05 PM
compiler generated destructor vs class implemented destructor arun C++ 2 06-13-2006 05:43 AM
Explicit destructor calls from inside base class destructor frs C++ 20 09-21-2005 09:22 AM



Advertisments