-
November 27th, 2010, 05:58 PM
#1
Confused about constructor initializations.
I encountered the following while reading some C++ code.
Consider these two code examples:
Code:
class Foo { // Declares class Foo
public:
int x; // Member variable
Foo(): x(0) { // Constructor for Foo,
} // initializes x
int bar(int i) { // Member function bar()
return 3*i + x;
}
};
Code:
class Foo { // Declares class Foo
public:
int x; // Member variable
Foo() { // Constructor for Foo,
x(0); // initializes x
}
int bar(int i) { // Member function bar()
return 3*i + x;
}
};
I don't know what the difference is between how x is initialized in each.
Are they the same thing, just multiple ways to do it?
-
November 27th, 2010, 06:50 PM
#2
Re: Confused about constructor initializations.
Originally Posted by jsg
Code:
Foo() { // Constructor for Foo,
x(0); // initializes x
}
That's not valid code. You can do x = 0; in the c'tor body.
For the difference between the two methods, see http://www.parashift.com/c++-faq-lit....html#faq-10.6
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
November 27th, 2010, 07:42 PM
#3
Re: Confused about constructor initializations.
Originally Posted by D_Drmmr
Yes, I meant to do x = 0; in the second example.
That article helped somewhat, I don't understand everything, but I do get the gist that initialization list are better to use than assignment in most cases.
-
November 30th, 2010, 01:59 PM
#4
Re: Confused about constructor initializations.
If the field was 'SomeType x' instead of 'int x', and SomeType didn't have a no-parameter constructor, then your only choice would be the 1st way.
-
November 30th, 2010, 02:16 PM
#5
Re: Confused about constructor initializations.
Originally Posted by jsg
Code:
Foo() { // Constructor for Foo,
x(0); // initializes x
}
Originally Posted by D_Drmmr
That's not valid code.
Incorrect, it is valid code, it just isn't doing what the OP expected. That is calling a method named x.
You are not allowed to do code like that because if it was allowed, callback functions would be unable to work. You can only use that syntax in very select cases. Constructor initialization lists is one of those cases.
Last edited by ninja9578; November 30th, 2010 at 02:18 PM.
-
December 1st, 2010, 11:47 AM
#6
Re: Confused about constructor initializations.
Originally Posted by ninja9578
Incorrect, it is valid code, it just isn't doing what the OP expected. That is calling a method named x.
You are not allowed to do code like that because if it was allowed, callback functions would be unable to work. You can only use that syntax in very select cases. Constructor initialization lists is one of those cases.
Actually in the context of the code written by the OP, D_Drmmr is correct, the code is invalid - it will not compile even if there is a function named x declared prior to Foo, because due to look up rules the compiler will try to match x(0) against the member variable first and since an int does not define operator()(int), the compilation will fail.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|