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.
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 (eg 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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.