? about the default constructor

Started by
39 comments, last by bluefox25 16 years, 11 months ago
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 :(
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.
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.
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).



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?
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 ?
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.
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 variablesaxis:axis(int a,int b){ // sets constructor   mx = a;   my = b;}

How many constructors can you have in any given clas? No limits?
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) { }

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.

This topic is closed to new replies.

Advertisement