473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Inheritance and destructors, guidance please

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

Jul 19 '05 #1
4 2793
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.

--
Attila aka WW
Jul 19 '05 #2
> 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.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 19 '05 #3
On Wed, 8 Oct 2003 11:08:45 +0200, "Peter van Merkerk"
<me*****@deadspam.com> wrote:
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 :)
Jul 19 '05 #4
<snip>
Thanks very much to you both, Precisely the answers I required.


Please, do not quote the whole post for thanks.
Jonathan
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mark | last post by:
When declaring a class that uses multiple inheritance, does the order used when listing the inheritance matter? I'm finding with my compiler (gcc 3.2.2) that my program seg faults when destructing...
3
by: Christopher Benson-Manica | last post by:
Okay, having purchased the excellent Josuttis book last night, I've almost got my little streambuf subclass bit worked out. Now, though, I'm faced with some issues with the destructors... ...
4
by: Busin | last post by:
When a child class inherits from a base class, will the child class inherits everything of the base class, including all member variables and functions? Or is such inheritance "selective", like not...
19
by: Mike Tyka | last post by:
Hello community, i'm fairly new to using the STL but i've been experimenting a bit with it. I tried to derive a new class say MyString from string like so: class MyString: public string{...
3
by: Michael | last post by:
Ok, I've got a Singleton template: template<class T> class Singleton { public: static T& GetInstance() { static T* pInst= NULL;
4
by: Dot net work | last post by:
I thought I'd be clever and create my own strongly typed array list, because I wanted to be sure that when an object was added to the arraylist, it would *only* be an object of a class that I had...
1
by: FefeOxy | last post by:
Hi, This is a hypothetical thing that I want to test out, and if it works, I will be using it to enhance my code. // --- Suppose I have an abstract base class: --- class A { public:...
3
by: dice | last post by:
This is a condensed version of a problem I came across today, can anyone explain what is going on? If the code below is run nothing will be output (in the original problem it was causing...
11
by: axel22 | last post by:
Please observe this simple model of multiple inheritance: void main() { class A { public: virtual void print() { cout << "A" << endl; }; class Support1 : virtual public A {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.