On Apr 15, 6:18 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Bushido Hacks wrote:
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?
Virtual or not doesn't change anything here.
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[i];};
delete[] p;
};
Derived::~Derived(){
// Should these lines be here?
for(int i = 0; i < m; i++){delete[] p[i];};
delete[] p;
No, definitely not. The destructor for the base class will
take care of that.
};
The general rule -- the owner should take care of deleting
anything it owns.
Just an idea, but I rather suspect that he simply wasn't
realizing that the derived class destructor always calls the
base destructor when it is finished, so he doesn't have to do
anything special with regards to the base class.
--
James Kanze (Gabi Software) email:
ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34