Jump to content
  • Advertisement
Sign in to follow this  
bluefox25

? about the default constructor

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

I hate having to start a thread for just for one simple ?. Wish there was an ongoing thread where you can ask newbie questions, oh well, here goes: A)When defining a default constructor I know it is legal to do this: Class::Class():mVar1(something),mVar2(something){} B)If there is nothing else but is this still legal Class::Class():mVar1(something),mVar2(something){ cout << "Some cool statement..."; } C)or would I need to do this: Class::Class(){ cout << "Some cool text"; mVar1(something); mVar2(something); } Not quite sure. My first guess would be B but I've been wrong before :(

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by bluefox25
B)If there is nothing else but is this still legal
Class::Class():mVar1(something),mVar2(something){
cout << "Some cool statement...";
}

Yes. This calls mVar1 and mVar2's constructors.

Quote:
C)or would I need to do this:
Class::Class(){
cout << "Some cool text";
mVar1(something);
mVar2(something);
}

No. This would call mVar1 and mVar2's operator(), assuming they had appropriate ones, which is entirely different.

Share this post


Link to post
Share on other sites
So when defining the default constructor, you need to always place the member variable assignments before the {}?
Class::Class():mVariablex(){}
I thought it was only to save space that you could writ it like that.

Share this post


Link to post
Share on other sites
Quote:
Original post by bluefox25
So when defining the default constructor, you need to always place the member variable assignments before the {}?


No. Member variable assignments must be placed inside the constructor body, and they use operator = anyway. The list placed before the body is called the initializer list, and it contains the initializations of the members.

Note that members are always initialized, so the initializer list only helps you specify which constructor they are initialized with (in place of their default constructor).



Share this post


Link to post
Share on other sites
Quote:
Original post by bluefox25
So when defining the default constructor, you need to always place the member variable assignments initializers before the {}?

Yes (for the fixed terminology -- although you can omit any initializations where you're fine with just the default, as they will, well, default :D). (Assignment uses '=', and is a bit different if the class does anything fancy -- usually just potentially a bit more expensive).



Quote:
I thought it was only to save space that you could writ it like that.

If B and C were equivilant, there wouldn't be a space saving:
Class::Class():mVariablex(){}
Class::Class(){mVariablex();}


See?

Share this post


Link to post
Share on other sites
Sorry that is what I meant, initialization, so whatever I place before the braces, will be the default value for the member variables, unless I assign a value to those member values:
If I have:

Class axis{
int x;
int yl
axis();
}
Class::axis():x(5),y(4){}//set/initialize member variables
//So if I declar:
axis myAxis;//This will set x(5) & y(4) right?
//unless I specify values like this:
axis myAxis(10,25);



Is this correct ?

Share this post


Link to post
Share on other sites
Given

Class axis{
int x;
int yl
axis();
}
Class::axis():x(5),y(4){}//set/initialize member variables
//So if I declar:
axis myAxis;//This will set x(5) & y(4) right?

you are correct that x and y is set to 5 and 4, respectively. The default constructor will initialize the member to those values. However,

//unless I specify values like this:
axis myAxis(10,25);

is not correct. Since you only provided a default constructor, you cannot initialize the object with a non-default constructor. You haven't provided a contructor taking any parameters. So the last one is invalid code.

Share this post


Link to post
Share on other sites
Ahhh, now I see. That is why including a constructor with parameters is needed. If I added another constructor like:

Class axis{
public:
axis();//default constructor
axis(int a,int b);//condstructor
private:
int mx;
int my;
}
axis::axis():x(5),y(4){}//set/initialize member variables
axis:axis(int a,int b){ // sets constructor
mx = a;
my = b;
}


How many constructors can you have in any given clas? No limits?

Share this post


Link to post
Share on other sites
No limits.

Although you should use initialize lists for all constructors, not just the default one.

axis:axis(int a,int b) : mx(a), my(b) { }

Share this post


Link to post
Share on other sites
There is no limit to the number of constructors you can write. Also:

axis::axis(int a,int b) : mx(a), my(b) {}

EDIT: darn, I've been out-brothered.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!