W
Warwick
Hi
I could use some help with inheritance and destructors.
I have been following an example in a book and the example has lead to
a situation which I have
reduced to a contrived example that follows.
B is derived from A, and C is derived from B.
All the derivations are public.
B has nothing to do on destruction. C and A do.
It seems to work, even tho B did not override the destructor declared
virtual in A. However I dont know if its leaking memory or not since I
dont really know how to test for that.
I have 3 related questions.
class A
{
char* MemSpace;
protected:
virtual int fooA ();
public:
A () { MemSpace = new char[256];}
virtual ~A (); //can't define here right?
};
A::~A() //A's destructor
{
delete MemSpace;
}
class B : public A
{
public:
B () : A::A() {}
};
class c : public B
{
char* MoreSpace;
protected:
virtual int fooA ();
public:
C () :B::B() { MoreSpace = new char[256];}
virtual ~C ();
};
Q. If B has not defined a destructor what happens if
{
B* tmp = new B();
//and later
delete tmp;
}
Will A's destructor be called automatically?
Q Writing C's destructor.
Should I write
~C ()
{
delete MoreSpace;
A:~A();
}
or
~C ()
{
delete MoreSpace;
delete this;
}
or
~C ()
{
delete MoreSpace;
delete dyanmic_cast<A*>(this);
}
or
something else?
Q Overriding fooA.
is
int C:fooA ()
{
//do stuff
A:fooA (); // call A's fooA.
}
the same as
int C:fooA ()
{
//do stuff
B:fooA (); // call B's surfaced fooA which calls A's fooA.
}
?
Any guidance, help is very much appreciated.
Best
Warwick
I could use some help with inheritance and destructors.
I have been following an example in a book and the example has lead to
a situation which I have
reduced to a contrived example that follows.
B is derived from A, and C is derived from B.
All the derivations are public.
B has nothing to do on destruction. C and A do.
It seems to work, even tho B did not override the destructor declared
virtual in A. However I dont know if its leaking memory or not since I
dont really know how to test for that.
I have 3 related questions.
class A
{
char* MemSpace;
protected:
virtual int fooA ();
public:
A () { MemSpace = new char[256];}
virtual ~A (); //can't define here right?
};
A::~A() //A's destructor
{
delete MemSpace;
}
class B : public A
{
public:
B () : A::A() {}
};
class c : public B
{
char* MoreSpace;
protected:
virtual int fooA ();
public:
C () :B::B() { MoreSpace = new char[256];}
virtual ~C ();
};
Q. If B has not defined a destructor what happens if
{
B* tmp = new B();
//and later
delete tmp;
}
Will A's destructor be called automatically?
Q Writing C's destructor.
Should I write
~C ()
{
delete MoreSpace;
A:~A();
}
or
~C ()
{
delete MoreSpace;
delete this;
}
or
~C ()
{
delete MoreSpace;
delete dyanmic_cast<A*>(this);
}
or
something else?
Q Overriding fooA.
is
int C:fooA ()
{
//do stuff
A:fooA (); // call A's fooA.
}
the same as
int C:fooA ()
{
//do stuff
B:fooA (); // call B's surfaced fooA which calls A's fooA.
}
?
Any guidance, help is very much appreciated.
Best
Warwick