Original title: ENUM VS Const
Original author: Rob Manderson
Original source: http://www.codeproject.com/cpp/enumvsconst.asp
Translation Author: xlander
Recently, I am responsible for reviewing some C code written by others. The code check can cause errors. If an error occurs, some kind of reasonable reason is performed. Written very well, but, then a small place made me depressed.
For some reason, I am not convenient here to show the real class. For the convenience of the problem, we come to manually.
The class I review is about the following:
Class CaClass
{
PUBLIC:
Const uint aconstantofinteresttothisclass1 = 0;
Const uint aconstantofinteresttothisclass2 = 1;
CaClass (Uint ConstValue, LPCTSTSTHOTHERPARETER);
Void Dosomething ();
Private:
Uint m_const;
CString M_Parameter;
}
The constructor of the class CACLASS is incorporated into a constant parameter and a string pointer it interested. DOSMETHING () Do some processing based on the parameters incorporated by the constructor. The implementation code of this class is as follows:
Caclass :: Caclass (uint constvalue, lpctstr sometherparameter)
{
M_Const = ConstValue;
m_parameter = someotherparameter;
}
Void Caclass :: DOSMETHING ()
{
Switch (m_const)
{
Case aconstantofinteresttothisclass1:
// do something defined by this constant.
Break;
Case aconstantofinteresttothisclass2:
// do something defined by this constant.
Break;
DEFAULT:
// it's not a valid value, raise an exception
RaiseException (Error);
Break;
}
}
This code can work properly, then where is the problem?
What will I do if it is me?
Class CBClass
{
PUBLIC:
ENUM Constsofinterestttothisclass
{
BConstantofinterestTothisclass1 = 0,
BConstantofinterestTTHISCLASS2 = 1,
}
CBClass (Constsofinteresttothisclass Constvalue,
LPCTSTSTR SOMEOTHERPARAMETER);
Void Dosomething ();
Private:
ConstsofinterestTTHISCLASCLASS M_CONST;
CString M_Parameter;
}
Implement code:
Cbclass :: Cbclass (Constsofinteresttothisclass Constvalue,
LPCTSTR SOMEOTHERPARAMETER)
{
M_Const = ConstValue;
m_parameter = someotherparameter;
}
Void cbclass :: DOSMETHING () {
Switch (m_const)
{
Case BconstantofinterestTTTHISCLASS1:
// do something defined by this constant.
Break;
Case BconStantofinterestTTHISCLASS2:
// do something defined by this constant.
Break;
DEFAULT:
// it's not a valid value, raise an exception
RaiseException (Error);
Break;
}
}
This has little difference, and any C compiler can produce the same code as the two classes.
So where is the difference?
The first class, CACLASS, defines a set of constants to themselves. The second class also defines a set of constants, but its form is Enum. ENUM can be used to define a limited set of legal values, or it can be used as a Pseudo DataType to see a difference between two constructor:
CaClass (Uint ConstValue, LPCTSTSTHOTHERPARETER);
CBClass (ConstsofinterestTothisclass Constvalue, LPCTSTR SOMEOTHERPARETER);
Caclass can accept any legal UINT values, in close 4 billion possible values, only two are interested in this class. Any UINT value except 0 and 1 will make DOSMETHING anomalous. Instead, CBCLASS can only accept one of two enumerated values. Passing any illegal constants will make the compiler to report errors or warnings.
Compare:
Caclass Obj (1000, "" this is a string "));
Obj.dosomething ();
Cbclass Obj (1000, _T ("this is a string);
Obj.dosomething ();
In the first example, CaClass Obj (1000, _T ("this is a string"); compile normally and throws an exception at runtime. In the second example, CBClass Obj ("this is a string); will not be polite to generate an error message in your compiler, so you don't generate executable files until you correct the error And provide legal values.
The constructor of class CBClass requires the type of the first parameter is constsofinteresttothisclass, which can be BConstantofinterestTTTHISCLASS1, or BConstantofinterestTTTHISCLASS2, but also a variable of the constsofinteresttothisclass type. The compiler allows you to define a variable of the ConstsOfinterestTotHisclass type, but you can only use the enumeration values you previously defined when you assign this variable.
Constsofinterestttothisclass var;
Var = bconstantofinteresttothisclass1; // ok
VAR = 47; // error
in conclusion:
The compiler can make numerous error checks for you when compiling, instead of const, enable the compiler to find code that uses an inappropriate assumption condition.