Class Inheritance and Constructors/Destructors

Started by
12 comments, last by Jason2Jason 21 years, 7 months ago
Hi, i did a search in the forums about this but didn''t find a clear answer. Say I have a base class: CBase, and it had a constructor to init some variables. Then I derive a class: CDerived : public CBase, which aslso has a constructor to init some of its own vars, will the first constructor get called then the constructor in the derived class be called next? Or will I have to rewrite the first constructer in the derived class? What about destructors? Will the base one be called then the new one in the derived class? Thanks, -J _____________________________________________ :S Confused? Me too! :S
Advertisement
You have the constructor CBase() for the class CBase. Your class CDerived has a constructor CDerived(). You have to call the base class's constructor at the end of the initialization in CDerived().
Example:


      CBase::CBase(){// Init member vars}...CDerived::CDerived(){// Init member varsCBase::CBase();}      


[edited by - LordLethis on September 3, 2002 1:49:08 PM]
[My Lousy Page | Kings Of Chaos | Vampires | [email=lordlethis@hotmail.com]email.me[/email]]
Actually, no:


            #include <iostream>using namespace std;class Base {public:	Base() { cout << "Base Constructor\n"; }	~Base() { cout << "Base Destructor\n"; }};class DerivedA : public Base {public:	DerivedA() { cout << "DerivedA Constructor\n"; }	~DerivedA() { cout << "DerivedA Destructor\n"; }};class DerivedB : public Base {public:	DerivedB() { cout << "DerivedB Constructor\n"; }	~DerivedB() { cout << "DerivedB Destructor\n"; }};class DerivedAB : public DerivedA, DerivedB{public:	DerivedAB() { cout << "DerivedAB Constructor\n"; }	~DerivedAB() { cout << "DerivedAB Destructor\n"; }};int main(void){	DerivedAB a;	cout << "\n";	return 0;}            


Output:

Base ConstructorDerivedA ConstructorBase ConstructorDerivedB ConstructorDerivedAB ConstructorDerivedAB DestructorDerivedB DestructorBase DestructorDerivedA DestructorBase Destructor      


(only happens with empty constructors afaik)

Of course, you could have done this yourself

[edit]Typo[/edit]

[edit]
If you call the base-class constructor in the derived constructor (using class DerivedA) the output is:

Base ConstructorDerivedA ConstructorBase ConstructorBase DestructorDerivedA DestructorBase Destructor 


[/edit]

Gyzmo
==============================
"Well is the world standard" - The Alchemist
"Not in Canada!" - jonnyfish

[edited by - gyzmo on September 3, 2002 2:08:18 PM]
Gyzmo=============================="Well is the world standard" - The Alchemist"Not in Canada!" - jonnyfish
Thats just confused me more!! Does the base constructor get called automaticly and then the constructor in the derived class? OR do I need to call it in the new constructor?

-J

_____________________________________________

:S Confused? Me too! :S
If you don't specificly select a base constructor, the default base constructor will be called from the derived (at the very, very beginning). However, the base constructor is called before the derived ctor. To explictly select a base ctor, use the

Derived::Derived() : Base(50){...}

syntax.

HTH,

Cédric

[edited by - cedricl on September 3, 2002 2:48:23 PM]
I thought it was pretty obvious, the base class'' constructor gets called automatically before the derived class'' constructor, and the objects are destroyed in reverse order.

Gyzmo
==============================
"Well is the world standard" - The Alchemist
"Not in Canada!" - jonnyfish
Gyzmo=============================="Well is the world standard" - The Alchemist"Not in Canada!" - jonnyfish
I must say, it would have been better if you had experimented yourself to get the answer, writing those small test-programs is an important habit for programmers to learn. Besides sometime improving your typing skill , it might give you insights into the workings of the compiler, it gives you more experience writing code allowing you to write less buggy code and it helps gaining new ideas of doing things.

Just my €0.02

Gyzmo
==============================
"Well is the world standard" - The Alchemist
"Not in Canada!" - jonnyfish
Gyzmo=============================="Well is the world standard" - The Alchemist"Not in Canada!" - jonnyfish
I am writing my very first derived class, and when i was nearly done, i thought I''d ask here about the constructor bit, which would save me the hassle of fixing bugs that might not be there (you know, like when you leave a ; and 100s of other errors are generated because of that one ; )
Well I get it now (i think) and will try it out.

Thanks,

-J
quote:Original post by LordLethis
You have to call the base class''s constructor at the end of the initialization in CDerived().


Jesus, man, what were you thinking? Have you ever actually done this??

J2J, here''s how it works. Since a derived class builds on the functionality of the base class it inherits, and can access member functions of the base class, it would be unsafe for the derived constructor to be called if the base constructor had not already been called. So C++ first goes through the base class constructor, and then it calls the derived constructor. (If you want the base constructor to be called with other than default arguments, look into "initialization lists".) Destructors work in reverse; first the derived class is destroyed, then the base class. All of this happens for you automatically (with the exception of virtual destructors; viz. any good C++ book).


Don''t listen to me. I''ve had too much coffee.
No, the derived constructor gets called first, then the default base constructor (BaseClass(); {}).

If you want to use any other constructor than the default you have to initialize it in the derived constructor like this:

DerivedClass::DerivedClass(...) : BaseClass(...)
{
//code...
}

-----------------------------
Valkyrias: Tears of Valkyries
-----------------------------Final Frontier Trader

This topic is closed to new replies.

Advertisement