Reputation: 71
I not very familiar with C++, have some issues with the multiple assignments
int a = 0, b = 0;
works
int i;
double d;
char c;
c = i = d = 42;
also works but why this does not works.
int a = 4, float b = 4.5, bool ab = TRUE;
Upvotes: 1
Views: 149
Reputation: 211580
This is to do with the allowed syntax. The format is, in very high-level terms, roughly:
type varName [ = expr ][, varName [ = expr ] ]?;
Where there is no allowance at all for another type to be introduced mid-stream.
In practice declaring multiple variables per line can lead to a lot of ambiguity and confusion.
Quick quiz: What is the initial value of a
? What type is b
?
int* a, b = 0;
If you guessed a
was a NULL pointer and b
was int*
you'd be wrong. This is why it's often preferable to spell it out long-form so there's absolute clarity:
int* a;
int b = 0;
Upvotes: 4