Destructor thru parent class

Discussion in 'C++' started by Sashi, Nov 30, 2006.

  1. Sashi

    Sashi Guest

    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
     
    Sashi, Nov 30, 2006
    #1
    1. Advertising

  2. Sashi

    mlimber Guest

    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-lite/virtual-functions.html#faq-20.7

    Cheers! --M
     
    mlimber, Nov 30, 2006
    #2
    1. Advertising

  3. Sashi

    dasjotre Guest

    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.
     
    dasjotre, Nov 30, 2006
    #3
  4. Sashi

    dasjotre Guest

    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-lite/basics-of-inheritance.html
     
    dasjotre, Nov 30, 2006
    #4
  5. Sashi

    Sashi Guest

    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
     
    Sashi, Nov 30, 2006
    #5
  6. Sashi

    dasjotre Guest

    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.
     
    dasjotre, Nov 30, 2006
    #6
  7. Sashi

    mlimber Guest

    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
     
    mlimber, Nov 30, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. frs
    Replies:
    20
    Views:
    759
    Alf P. Steinbach
    Sep 21, 2005
  2. arun
    Replies:
    2
    Views:
    547
    benben
    Jun 13, 2006
  3. Jimmy Hartzell
    Replies:
    0
    Views:
    421
    Jimmy Hartzell
    May 19, 2008
  4. THTB
    Replies:
    0
    Views:
    192
  5. Max Williams
    Replies:
    3
    Views:
    173
    Robert Klemme
    Jan 6, 2009
Loading...

Share This Page