-
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 ?
-
December 10th, 2008, 09:18 AM
#2
Re: Constant class members in case statements
>> case expression is not constant
And it needs to be. This means that the compiler must know the value of "i" at compile time. If "i" isn't constant, then the compiler can't know the value. Use an if statement and "i" can't be constant.
Also, variables with a leading underscore are reserved for "the system".
gg
-
December 10th, 2008, 09:36 AM
#3
Re: Constant class members in case statements
case label value is required to be not a constant (like, constant variable), but integral constant-expression, and these are, among others, literal values, or constant valiables initialized with literal values. const data member is not integral constant expression, because it might be initialized with non-literal value (i.e. via initialization list in constructor), so it cannot be used in case label.
Cheers
Last edited by Hobson; December 10th, 2008 at 09:41 AM.
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
December 10th, 2008, 09:46 AM
#4
Re: Constant class members in case statements
Thanks for your answers !
Originally Posted by Hobson
case label value is required to be not a constant (like, constant variable), but integral constant-expression, and these are, among others, literal values, or constant valiables initialized with literal values
Could you please explain with an example what you meant by "const variables initialized with literal values" ?
-
December 10th, 2008, 09:48 AM
#5
Re: Constant class members in case statements
Originally Posted by Codeplug
>> case expression is not constant
And it needs to be. This means that the compiler must know the value of "i" at compile time. If "i" isn't constant, then the compiler can't know the value. Use an if statement and "i" can't be constant.
gg
'i' is indeed constant because I have declared the variable as [const int i ] in the private section of the class declaration.
How is it that a member declaration of [static const int i = 10] works without a problem but not [const int i]
-
December 10th, 2008, 09:55 AM
#6
Re: Constant class members in case statements
Sure, here it is:
Code:
#include <string>
#include <vector>
#include <iostream>
using std::cin;
using std::cout;
int function() {return 5;}
int main() {
//constant variable initialized with literal value
const int label_literal_val = 10;
//constant variable initialized with expression value
const int label_function_val = function();
int number;
cin >> number;
switch(number) {
//no error, case label value is a constant expression
//(constant variable initialized with literal value)
case label_literal_val:
cout << "'literal' case\n";
break;
//error, case label value is not a constant expression
//constant variable initialized other way than literal
case label_function_val:
cout << "'function' case\n";
break;
default:
cout << "Default case\n";
break;
}
}
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
December 10th, 2008, 10:05 AM
#7
Re: Constant class members in case statements
Originally Posted by humble_learner
'i' is indeed constant because I have declared the variable as [const int i ] in the private section of the class declaration.
How is it that a member declaration of [static const int i = 10] works without a problem but not [const int i]
A member variable may differ between instances. Therefore it is not really a constant.
Remember a switch statement must be convertable into an array of "gotos" (jumps). This array is build by the compiler ONCE at compile time. So if you have:
Code:
swtich (X)
{
case 0: break;
case 2: break;
case 3: break;
default: break;
}
The compiler will generate a 4 element array with
Code:
[0] = "goto case 0:";
[1] = "goto default:";
[2] = "goto case 2:";
[3] = "goto case 3:";
This table is immutable (it can not be changed ar runtime).
Now it should be obvious why each case value must be a hard compile time contant integral expression.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
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
|