Archived

This topic is now archived and is closed to further replies.

Class Inheritance and Constructors/Destructors

This topic is 5584 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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 vars

CBase::CBase();
}


[edited by - LordLethis on September 3, 2002 1:49:08 PM]

Share this post


Link to post
Share on other sites
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 Constructor
DerivedA Constructor
Base Constructor
DerivedB Constructor
DerivedAB Constructor

DerivedAB Destructor
DerivedB Destructor
Base Destructor
DerivedA Destructor
Base 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 Constructor
DerivedA Constructor
Base Constructor
Base Destructor

DerivedA Destructor
Base 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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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
quote:
Original post by biovenger
No, the derived constructor gets called first, then the default base constructor (BaseClass(); {}).



You''re wrong. Have you even tried this?

Here''s some proof code.


  
#include <iostream>

class CBase
{
public:
CBase() { std::cout << "CBase::CBase()\n"; }
};

class CDerived : public CBase
{
public:
CDerived() { std::cout << "CDerived::CDerived()\n"; }
};

int main()
{
CDerived myClass;

return 0;
}


Gosh, it runs the base-class ctor first! fancy that!



Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
HELLOOOOO!!!!! has anybody paid any attention to my first post? I think it has illustrated it pretty conclusively, why are you still discussing this?

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

Share this post


Link to post
Share on other sites
If Gyzmo didn''t clear it up for you all, just write a small test app for yourselves.

// Create an object here, and the compiler will call these for you, in this order
CBase ()
CDerived ()
// It goes out of scope or gets deleted, the compiler will call these for you, in this order
~CDerived ()
~CBase ()

You should never have to call a destructor manually. You normally never have to call an constructor manually.

Also, always make your base class destructor virtual. Otherwise the derived one will not be called when you do this:

Base * p = new Derived;
delete p;


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites