Reputation: 11
In the following code:
class Class
{
private:
LUID luid;
public:
Class()
{
luid = { 0, 0}; // A. Does not compile
LUID test = {0, 0}; // B. Compiles
test = {1,1}; // C. Does not compile
}
Why are A and C not right, but B is fine?
The error I get for A and C is:
error C2059: syntax error : '{'
error C2143: syntax error : missing ';' before '{'
error C2143: syntax error : missing ';' before '}'
I think it has to do with the C++ version, although I am not sure which version this is using besides it not being very new.
Upvotes: 0
Views: 155
Reputation: 35154
Statement LUID test = {0, 0}
is an initialization of a local variable using an initialization list; this is valid, as it is used in the course of a variable definition. test = {0, 0}
, in contrast, is an assignment, as test
is defined elsewhere. Assigning initializer lists is supported only in particular cases (e.g. when assigning to a scalar or to a particular sort of class type (cf., for example, braced-init-list assignment).
Other cases, like, for example, arrays, cannot be assigned but just initialized:
typedef int LUID[2];
int main(){
LUID t = { 10, 20 }; // compiles
// t = { 10, 20}; // does not compile, since an array is not assignable
return 0;
}
Upvotes: 3