Derived Destructors

Discussion in 'C++' started by Bushido Hacks, Apr 15, 2007.

  1. I think this message got lost the first time I sent it, so I'm
    reposting it.
    I need a little help with Destructors.
    Suppose I created a multidimmensional array in a Base class that was
    also accessable in a Derived class, would I need to delete the array
    in the Derived class if I declared my destructors as virtual?

    class Base
    {
    public:
    ...
    virtual ~Base();
    protected:
    int m; // rows
    int n; // cols
    double** p;
    };

    class Derived : public Base
    {
    public:
    ...
    virtual ~Derived();
    };

    Base::~Base()
    {
    for(int i = 0; i < m; i++){delete[] p;};
    delete[] p;
    };

    Derived::~Derived(){
    // Should these lines be here?
    for(int i = 0; i < m; i++){delete[] p;};
    delete[] p;
    };
     
    Bushido Hacks, Apr 15, 2007
    #1
    1. Advertising

  2. Bushido Hacks wrote:
    > I think this message got lost the first time I sent it, so I'm
    > reposting it.


    It didn't. I replied. The answer is 'now'.

    > [..]


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 15, 2007
    #2
    1. Advertising

  3. Bushido Hacks wrote:
    > I think this message got lost the first time I sent it, so I'm
    > reposting it.


    It didn't. I replied. The answer is "no, it shouldn't".

    > [..]


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 15, 2007
    #3
  4. Bushido Hacks

    simonod Guest

    On Apr 15, 6:41 pm, "Bushido Hacks" <> wrote:
    > I think this message got lost the first time I sent it, so I'm
    > reposting it.
    > I need a little help with Destructors.
    > Suppose I created a multidimmensional array in a Base class that was
    > also accessable in a Derived class, would I need to delete the array
    > in the Derived class if I declared my destructors as virtual?
    >
    > class Base
    > {
    > public:
    > ...
    > virtual ~Base();
    > protected:
    > int m; // rows
    > int n; // cols
    > double** p;
    >
    > };
    >
    > class Derived : public Base
    > {
    > public:
    > ...
    > virtual ~Derived();
    >
    > };
    >
    > Base::~Base()
    > {
    > for(int i = 0; i < m; i++){delete[] p;};
    > delete[] p;
    >
    > };
    >
    > Derived::~Derived(){
    > // Should these lines be here?
    > for(int i = 0; i < m; i++){delete[] p;};
    > delete[] p;
    >
    >
    >
    > };- Hide quoted text -
    >
    > - Show quoted text -


    Hi,

    For me, the quick answer is you always delete where you new. In your
    case, if you new'd the array in the base class then you also delete it
    there (and nowhere else). The fact that the destructor is virtual
    makes no difference to this fact: what that does is to make sure that
    delete calls the correct destructor of an object even if you only have
    a pointer to a base class of that object. Correctly calling a derived
    class destructor will still mean that the base class destructor gets
    called too, and you're memory will be free'd.

    Hope that helps,
    Si
     
    simonod, Apr 15, 2007
    #4
  5. On Apr 15, 2:01 pm, "simonod" <> wrote:
    > On Apr 15, 6:41 pm, "Bushido Hacks" <> wrote:
    >
    >
    >
    > > I think this message got lost the first time I sent it, so I'm
    > > reposting it.
    > > I need a little help with Destructors.
    > > Suppose I created a multidimmensional array in a Base class that was
    > > also accessable in a Derived class, would I need to delete the array
    > > in the Derived class if I declared my destructors as virtual?

    >
    > > class Base
    > > {
    > > public:
    > > ...
    > > virtual ~Base();
    > > protected:
    > > int m; // rows
    > > int n; // cols
    > > double** p;

    >
    > > };

    >
    > > class Derived : public Base
    > > {
    > > public:
    > > ...
    > > virtual ~Derived();

    >
    > > };

    >
    > > Base::~Base()
    > > {
    > > for(int i = 0; i < m; i++){delete[] p;};
    > > delete[] p;

    >
    > > };

    >
    > > Derived::~Derived(){
    > > // Should these lines be here?
    > > for(int i = 0; i < m; i++){delete[] p;};
    > > delete[] p;

    >
    > > };- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > Hi,
    >
    > For me, the quick answer is you always delete where you new. In your
    > case, if you new'd the array in the base class then you also delete it
    > there (and nowhere else). The fact that the destructor is virtual
    > makes no difference to this fact: what that does is to make sure that
    > delete calls the correct destructor of an object even if you only have
    > a pointer to a base class of that object. Correctly calling a derived
    > class destructor will still mean that the base class destructor gets
    > called too, and you're memory will be free'd.
    >
    > Hope that helps,
    > Si


    What if the base class and the derived class both have a assignment
    operator functions? If I use the delete and new functions in the
    derived definition, do I still need those lines in Derived's
    destructor?
     
    Bushido Hacks, Apr 15, 2007
    #5
  6. Bushido Hacks

    Zeppe Guest

    Bushido Hacks wrote:
    > On Apr 15, 2:01 pm, "simonod" <> wrote:
    >> On Apr 15, 6:41 pm, "Bushido Hacks" <> wrote:
    >>
    >>
    >>
    >>> I think this message got lost the first time I sent it, so I'm
    >>> reposting it.
    >>> I need a little help with Destructors.
    >>> Suppose I created a multidimmensional array in a Base class that was
    >>> also accessable in a Derived class, would I need to delete the array
    >>> in the Derived class if I declared my destructors as virtual?
    >>> class Base
    >>> {
    >>> public:
    >>> ...
    >>> virtual ~Base();
    >>> protected:
    >>> int m; // rows
    >>> int n; // cols
    >>> double** p;
    >>> };
    >>> class Derived : public Base
    >>> {
    >>> public:
    >>> ...
    >>> virtual ~Derived();
    >>> };
    >>> Base::~Base()
    >>> {
    >>> for(int i = 0; i < m; i++){delete[] p;};
    >>> delete[] p;
    >>> };
    >>> Derived::~Derived(){
    >>> // Should these lines be here?
    >>> for(int i = 0; i < m; i++){delete[] p;};
    >>> delete[] p;
    >>> };- Hide quoted text -
    >>> - Show quoted text -

    >> Hi,
    >>
    >> For me, the quick answer is you always delete where you new. In your
    >> case, if you new'd the array in the base class then you also delete it
    >> there (and nowhere else). The fact that the destructor is virtual
    >> makes no difference to this fact: what that does is to make sure that
    >> delete calls the correct destructor of an object even if you only have
    >> a pointer to a base class of that object. Correctly calling a derived
    >> class destructor will still mean that the base class destructor gets
    >> called too, and you're memory will be free'd.
    >>
    >> Hope that helps,
    >> Si

    >
    > What if the base class and the derived class both have a assignment
    > operator functions? If I use the delete and new functions in the
    > derived definition, do I still need those lines in Derived's
    > destructor?
    >


    No, if you delete in the base, it's ok. What sigmonod was trying to tell
    you is that if the array is defined in your base class, and is allocated
    in your base class, no allocation or deallocation should be done in the
    derived class. It's a matter of good programming, not of C++
    constraints. You have to try to keep things separated, and to manage
    objects in a single class. That is, if not REALLY needed, avoid
    protected member variables.

    Anyway, your original question actually made sense: the virtual
    destructor has a somewhat particular behaviour, because usually when a
    virtual method is invoked, only the derived one is actually executed.

    Regards,

    Zeppe
     
    Zeppe, Apr 16, 2007
    #6
    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. Mike
    Replies:
    1
    Views:
    382
    Alf P. Steinbach
    Jul 30, 2003
  2. qazmlp
    Replies:
    7
    Views:
    513
    Howard
    Jul 27, 2004
  3. Replies:
    4
    Views:
    436
    Alf P. Steinbach
    May 23, 2007
  4. Replies:
    1
    Views:
    419
    myork
    May 23, 2007
  5. Replies:
    1
    Views:
    405
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page