Home > Back-end >  A class member function of the switch in the case of constant expression problem, please guidance
A class member function of the switch in the case of constant expression problem, please guidance

Time:10-01

I encounter problems as follows: in which a is often data members of the class, and in one of the member function has a switch - case, put a as a case,
Why have appeared: not a constant expression error; Thought for a long time did not want to understand, the younger brother is a beginner,
Hope the great god to help guide the younger brother, thanked first,


#include
#include

using namespace std;

The class test
{

Public:

The test () : a (0) {};
Void switch_test ();

Private:
const int a;
};

Void test: : switch_test ()
{
Int I=0;

The switch (I)
{
Case 1:
Cout<& lt;" Case 1 is * "& lt; break;
case a:
Cout<& lt;" It is ok for the test "& lt; break;
}

return;
}




CodePudding user response:

Behind the case cannot use variable!

CodePudding user response:

Behind the case seems to only write constant expressions,,,

CodePudding user response:

Const int a; The a is a variable, but the value of this variable does not change, can be understood as a const variable,
Constants can define
12345;
# define CA 12;
Enum a {=89 a1, a2};

CodePudding user response:

Case grammar are behind must be constant expression, and can only be integer constant

CodePudding user response:

After a uninitialized, initialization,
Direct writing,,

Private:
The static const int a=1;

CodePudding user response:

But this usage is not good, if you are a really=1, then with case 1 repeat, there will be a problem, don't recommend that you write code, in this way
  • Related