Using a constant (from within the function) in a switch statement

Hi everyone,
So here's a problem I've encountered:
I want to create an object. That object is going to contain a function, which, let's say, checks whether you want to use the selected item on the menu, or just exit that menu.
Here's a prototype:
1
2
3
4
5
6
7
8
9
10
11
12
class options
{
public:
    int functionChangeColor(int typeOfColor);
private:
    static const int 
    functionTerminate(int result),
    ESCAPE_KEY=27,
    SPACEBAR_KEY=32,
    BACKGROUND=0,
    FONT=1;
};

Its function then identifies what it is that you want to change:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int options::functionChangeColor(int typeOfColor)
{
    while (1>0)
    {
        char pressedKey=getch();
        int result=functionDetectCommand(pressedKey);
        static const int TERMINATE=functionTerminate(result);
//"functionTerminate()" returns const int, and it basically checks whether you pressed
//the escape or space key.
        switch (result)
        {
            case ENTER_KEY:
            {
                break;
            }
            case TERMINATE:
            {
                return 1;
            }
        }
    }
}
const int options::functionTerminate(int result)
{
    return (result==ESCAPE_KEY || result==SPACEBAR_KEY);
}

Considering all of this, does anyone know why I'm getting this compiler error?

'TERMINATE' cannot appear in a constant-expression

It appears only when I declare the const inside the function.
If I declare it inside the definition of the class, then it won't be a problem.
Am I missing something?
After all, I thought it's the same thing to use a constant from within the function.
And also, even if not a static constant, TERMINATE still won't be accepted.
A case label must be a compile time constant expression.

Some documentation: http://www.cplusplus.com/doc/tutorial/control/

And from that documentation:
Notice that switch is limited to compare its evaluated expression against labels that are constant expressions. It is not possible to use variables as labels or ranges, because they are not valid C++ constant expressions.


Thanks, I didn't know that.
Topic archived. No new replies allowed.