Jump to content
  • Advertisement

Archived

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

BradDaBug

Stupid C++ question

This topic is 5728 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

If I have a base class with a constructor, then make another class inherited from the base class which has its own constructor, is the constructor of the base class executed?

Share this post


Link to post
Share on other sites
Advertisement
If you don''t specifically call a constructor, the default ctor for the base class will be called. If it doesn''t exist, you''ll get an error.

Share this post


Link to post
Share on other sites
You could test this by placing some kind of function in your base constructor that prints a message to the screen via printf or MessageBox and also place an identicle contructor for your derived class that does the same thing except one say, "Base contructor" while the other says, "Derived Constructor."

What are the results?

Share this post


Link to post
Share on other sites
yes, before the derived class constructor is entered.

if it needs anything passing to it you can do it in the initialisation list.

if there is more than one constructor for the base class you can choose it in the derived class constructor.

ie

class base {
int size_
public:
base(int size);
};

base::base(int size)
: size_(size) // initialiser lists are efficient way
{ // to initialise your class members
}

class derived : public base {
string name_;
public:
derived(int size, string name);
};

derived::derived(int size, string name)
: base(size), name_(name)
{
}


[edited by - petewood on January 17, 2003 4:44:16 PM]

Share this post


Link to post
Share on other sites
The base class' constructor is always called before you enter the derived class' constructor. You can and should also call it explicitly:

CDerivedClass::CDerivedClass(int some_arg)
: CBaseClass(some_arg)
{
// TODO: something...
}


The stuff between the colon (:) and the function body is the Constructor Initializer List (or something like that ). You use it to initialize the eventual base class, and also member variables.

EDIT: post redundant due to petewood




[edited by - CWizard on January 17, 2003 4:50:09 PM]

Share this post


Link to post
Share on other sites
And if you want the derived constructor to be called, use
virtual 
in front of the base class constructor.



tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)

Share this post


Link to post
Share on other sites
That''s for destructors, not for constructors. At construction time you must know the type, so a virtual is .... doesn''t make sense.

-----------------------------
Gamedev for learning.
libGDN for putting it all together.

Share this post


Link to post
Share on other sites
quote:
Original post by BradDaBug
So that means the base constructor will be called, but the constructor for the inherited class never will be?
Yes it will. The constructor of a inherited class will be called when the base object is fully constructed.

class A
{
public: A() { std::cout << "Constructing class A...\n"; }
~A() { std::cout << "Destructing class A...\n"; }
};

class B : public A
{
public: B() { std::cout << "Constructing class B...\n"; }
~B() { std::cout << "Destructing class B...\n"; }
}

int main(void)
{
B b_obj;
return 0;
}

Running this will output:

Constructing class A...
Constructing class B...
Destructing class B...
Destructing class A...

You can also call the base constructor explicitly this way:
B::B() : A() { std::cout << "Constructing class B...\n"; }


Share this post


Link to post
Share on other sites
quote:
Original post by Tazel
And if you want the derived constructor to be called, use
virtual   
in front of the base class constructor.

this is just not true. ignore and forget.

[edited by - petewood on January 18, 2003 7:01:26 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!