Inheritance and destructors, guidance please

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
 
A

Attila Feher

Warwick wrote:
[SNIP]
class A
{

char* MemSpace;
protected:
virtual int fooA ();

public:
A () { MemSpace = new char[256];}
virtual ~A (); //can't define here right?
};

You can. It will either make sense or not. The rule of thumb: don't define
it there is it can change (destructors can) or if it is unlikely to be
called on a non-pointer/reference... that one is quite specific for each
case. BTW it is not only "there" but any kind of inline definition.
class B : public A
{

public:
B () : A::A() {}

You don't need to write that A::A(), it is the default.
};

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?

Yes. And the compiler generated B destructor will override the one in A,
but it will also call the one in A.
Q Writing C's destructor.
Should I write

~C ()
{
delete MoreSpace;
A:~A();
}

Nope. The destructor of A will be called automagically. You do not need to
do anything.
or

~C ()
{
delete MoreSpace;
delete this;

No way. Don't ever write delete this in a destructor.
}
or

~C ()
{
delete MoreSpace;
delete dyanmic_cast<A*>(this);
}
or
something else?
Nope.

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.
}

?

They are equvivalent. It is better to use B::fooA, since then C will not
need to know about B inheriting from A.
 
P

Peter van Merkerk

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 (); file://can't define here right?
};

A::~A() file://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();
file://and later
delete tmp;
}

Will A's destructor be called automatically?

Yes, base class destructors will always be called, even if a derived
class does not implement its own destructor.
Q Writing C's destructor.
Should I write

~C ()
delete MoreSpace;
A:~A();
}

No, once C::~C() has finished A::~A() will automatically be invoked.
~C ()
{
delete MoreSpace;
delete this;
}

NO!!!!!!!! Don't do 'delete this' in the destructor!!!! This is never
correct, anything might happen if you do this though it would probably
lead to infinite recursion as 'delete this' will call the destructor of
class C...etc. After the destructor of a class instance has completed,
the memory that that instance occupies will be released.
or

~C ()
{
delete MoreSpace;
delete dyanmic_cast<A*>(this);
}

No, don't delete the base class, has the same effect as the previous
attempt.
or
something else?

C:: ~C ()
{
delete MoreSpace;
}

will do.
Q Overriding fooA.
is
int C:fooA ()
{
file://do stuff
A:fooA (); // call A's fooA.
}

the same as
int C:fooA ()
{
file://do stuff
B:fooA (); // call B's surfaced fooA which calls A's fooA.
}

It has the same effect because class B doesn't override fooA().
Any guidance, help is very much appreciated.

Maybe you need a better book, a good book should explain these things.
See www.accu.org for book recommendations.
 
W

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 (); file://can't define here right?
};

A::~A() file://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();
file://and later
delete tmp;
}

Will A's destructor be called automatically?

Yes, base class destructors will always be called, even if a derived
class does not implement its own destructor.
Q Writing C's destructor.
Should I write

~C ()
delete MoreSpace;
A:~A();
}

No, once C::~C() has finished A::~A() will automatically be invoked.
~C ()
{
delete MoreSpace;
delete this;
}

NO!!!!!!!! Don't do 'delete this' in the destructor!!!! This is never
correct, anything might happen if you do this though it would probably
lead to infinite recursion as 'delete this' will call the destructor of
class C...etc. After the destructor of a class instance has completed,
the memory that that instance occupies will be released.
or

~C ()
{
delete MoreSpace;
delete dyanmic_cast<A*>(this);
}

No, don't delete the base class, has the same effect as the previous
attempt.
or
something else?

C:: ~C ()
{
delete MoreSpace;
}

will do.
Q Overriding fooA.
is
int C:fooA ()
{
file://do stuff
A:fooA (); // call A's fooA.
}

the same as
int C:fooA ()
{
file://do stuff
B:fooA (); // call B's surfaced fooA which calls A's fooA.
}

It has the same effect because class B doesn't override fooA().
Any guidance, help is very much appreciated.

Maybe you need a better book, a good book should explain these things.
See www.accu.org for book recommendations.

Thanks very much to you both, Precisely the answers I required.

cheers :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top