Reputation: 2890
Consider the following class definition:
class C {
public:
int i = 9;
const int j = 2;
};
Without using the flag to enable C++11 when compiling (using g++, e.g. g++ -o test test.cpp
) the compiler complains on the member variable initializations. However, using -std=c++11
this works fine.
Why has this rule been changed in C++11? Is it considered bad practice initializing member variables in this manner?
Upvotes: 2
Views: 2207
Reputation: 263380
Initializing non-static data members at the point of their declaration wasn't just "bad practice"; it simply wasn't possible at all prior to C++11.
The equivalent C++03 code would be:
class C
{
public:
C() : i(9), j(2)
{
}
int i;
const int j;
};
And just in case: What is this weird colon-member syntax in the constructor?
Upvotes: 4