|
-
December 10th, 2008, 09:05 AM
#1
Constant class members in case statements
Hi,
I have a constant integer variable (i) as a class member of my Class X.
This variable (i) is initialized in the constructor of Class X.
However, why does the compiler prevent the use of the variable (i) in a case statement in the switch-case construct ? After all, is 'i' not a constant ?
The compiler error shown is
Error C2051 : case expression is not constant
Code:
X::X():i(10)
{
}
void X::MyFunction()
{
switch(_someValue)
{
case i :
{
}
};
}
This works if I had defined 'i' as a 'static const int' and initialized it at the point of class declaration.
Could you please explain the above difference ?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|