Skip to main content
gamedev.net
🔒 Locked

Stupid C++ question

Started by BradDaBug Jan 17, 2003 at 3:32 PM 15 replies 500+ views
Original Post
BradDaBug
BradDaBug
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?
I like the DARK layout!
aggregate
aggregate
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.
Like2Byte
Like2Byte
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?
Advice, eh? Well, besides working on your swing...you know, besides that...I'd have to think.
Qw3r7yU10p!
Qw3r7yU10p!
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]
CWizard
CWizard
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]
Tazel
Tazel
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)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
risingdragon3
risingdragon3
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.
BradDaBug
BradDaBug
So that means the base constructor will be called, but the constructor for the inherited class never will be?
I like the DARK layout!
CWizard
CWizard
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"; }


Qw3r7yU10p!
Qw3r7yU10p!
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]
Qw3r7yU10p!
Qw3r7yU10p!
you can''t have virtual constructors because you actually have to name the constructor you are calling

ie Base* pB = new Derived();

will call the Derived constructor. This in it''s part will construct a Derived object which is made up of the Base object and anything additional the Derived class adds. The Base class constructor is automatically called before entering the body of the Derived class constructor.

You could have a ''clone'' base member function which is virtual and can be overidden in derived classes. That''s about as close as you''ll get to a virtual constructor in c++.

So given some object which could be anywhere down a class hierarchy you can do this:

Base* pb = someFunctionReturningAPointer();
Base* pNewB = pb->clone();

you could use templates to make the coding of this easier but I''ll leave that for you to work out.
Tazel
Tazel
I''m sorry. I must have read the book wrong.



tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
Tazel
Tazel
Also, with this code


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


If you wanted to initialize stuff after if with other variables ect., would you go like this?


B::B() : A() { std::cout << "Constructing class B...\n"; }, int a(_a), int b(_b)?

Is this a rare instance in where a , follows a }?




tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
CWizard
CWizard
quote:
Original post by Tazel
If you wanted to initialize stuff after if with other variables ect., would you go like this?

B::B() : A() { std::cout << "Constructing class B...\n"; }, int a(_a), int b(_b)?

Is this a rare instance in where a , follows a }?
No it''s not (I don''t think there is such instance). You''re on the right track, though. Other member variables can be initialized before the constructor is entered, but I cannot imaging why you would want to initialize them after the constructor.

class A { public: A(int arg); ~A(); private: int data; };

class B : public A
{
public: B(int base_arg, int our_arg);
~B();
private: int our_data;
};

B::B(int base_arg, int our_arg)
: A(base_arg), our_data(our_arg)
{
// To have our_data in the initializer list, is equivalent of:
our_data = our_arg; // <-- now redundant
// blah...
}



Qw3r7yU10p!
Qw3r7yU10p!
quote:
B::B() : A() { std::cout << "Constructing class B...\n"; }, int a(_a), int b(_b)


can''t do it (doesn''t compile)

The initialisation list that comes after the constructor is only allowed before the main function.

B::B(int _a, int _b) : A(), a(_a), b(_b)// i assume a and b are members of B
{
//main body
}
Tazel
Tazel
So you have to the the function last. Then is the body of the constructor the body of the function?



tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
CWizard
CWizard
quote:
Original post by Tazel
So you have to the the function last. Then is the body of the constructor the body of the function?
I don''t understand your question really, but read this part of Thinking in C++ by Bruce Eckel.


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.