Destructors of Derived classes

Discussion in 'C++' started by Mike, Jul 30, 2003.

  1. Mike

    Mike Guest

    I seem to be having a problem with the way destructors are called with
    derived classes. Below is a short example of what I'm trying to do.
    In the example a is the base class. It has a function 'action' which
    destroys the object. As I understand derived classes and destructors,
    the destructor of the derived class should be called first, then the
    destructor of the base class. Really, for my purposes, order doesn't
    matter. In my example here I would only get the output "a:~a". It
    never executes the destructor of the derived class.

    The code below is just an example to simply show an idea, it hasn't
    been compiled.


    class a
    {
    public:
    action();
    ~a();

    };

    class a_derivative:a
    {
    public:
    ~a_derivative();

    };


    a:~a()
    {
    printf("a:~a");
    }

    a:action()
    {
    delete this;
    }


    a_derivative:~a_derivative()
    {
    printf("a_derivative:~a_derivative");
    }


    main()
    {
    a_derivative* my_a;

    my_a = new a_derivative();

    my_a->action();

    }
    Mike, Jul 30, 2003
    #1
    1. Advertising

  2. On 30 Jul 2003 08:44:36 -0700, (Mike) wrote:

    >I seem to be having a problem with the way destructors are called with
    >derived classes. Below is a short example of what I'm trying to do.
    >In the example a is the base class. It has a function 'action' which
    >destroys the object. As I understand derived classes and destructors,
    >the destructor of the derived class should be called first, then the
    >destructor of the base class. Really, for my purposes, order doesn't
    >matter. In my example here I would only get the output "a:~a". It
    >never executes the destructor of the derived class.
    >
    >The code below is just an example to simply show an idea, it hasn't
    >been compiled.
    >
    >
    >class a
    >{
    > public:
    > action();


    void action();



    > ~a();


    virtual ~a();




    >
    >};
    >
    >class a_derivative:a
    >{
    > public:
    > ~a_derivative();
    >
    >};
    >
    >
    >a:~a()
    >{
    > printf("a:~a");
    >}
    >
    >a:action()
    >{
    > delete this;
    >}
    >
    >
    >a_derivative:~a_derivative()
    >{
    > printf("a_derivative:~a_derivative");
    >}
    >
    >
    >main()


    int main()



    >{
    > a_derivative* my_a;
    >
    > my_a = new a_derivative();
    >
    > my_a->action();
    >
    >}
    Alf P. Steinbach, Jul 30, 2003
    #2
    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. Colin Goudie
    Replies:
    6
    Views:
    465
    Victor Bazarov
    Jan 26, 2004
  2. qazmlp
    Replies:
    7
    Views:
    490
    Howard
    Jul 27, 2004
  3. Bushido Hacks

    Derived Destructors

    Bushido Hacks, Apr 15, 2007, in forum: C++
    Replies:
    5
    Views:
    327
    Zeppe
    Apr 16, 2007
  4. Replies:
    1
    Views:
    393
    myork
    May 23, 2007
  5. Replies:
    1
    Views:
    385
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page