Below is code that declares two int variables and tries to use them in a switch statement. Is this a legal operation in C++? If not, why not?
int i = 0;
int x = 3;
switch (i)
{
case x:
// stuff
break;
case 0:
// other stuff
break;
}
The case
label must be an integral constant expression, so your example is invalid. But if x
were changed to:
const int x = 3;
then it's valid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With