I am only responsible for conversion! [C/C ++]
Written by Allen Lee
Not long ago, LSP left such a problem here:
Note:
1. An enumeration is defined as follows:
Enum _ Jb_prog_mode
{
Program_one= 0,
Program_two
} ;
Enum _ Jb_prog_mode e_prog_index;
2. Questions:
E_prog_index = ( Enum _ Jb_prog_mode) - 1 ;
E_prog_index equals-1? Why?
Your doubts hide the conclusion that the value of e_prog_index should not be equal to-1. I believe that the reason you come to this conclusion is that no member in the _ jb_prog_mode enumeration has a value of-1, so the value of the variable e_prog_index of the type of _ jb_prog_mode should not be-1.
Now, let's change the scenario. Assume that the registry contains the following information:
[HKEY_LOCAL_MACHINE \ SOFTWARE \ Allen]
"Name" = "Allen Lee"
"ID" = DWORD: 00000584
How can we obtain this information through programming? We can use the regqueryvalueex () function of Win32 API to read the information (CodeSegment ):
Lpbyte lpdata;
DWORD databuflen;
If (Regqueryvalueex (hkey, // Hkey is the handle pointing to HKEY_LOCAL_MACHINE \ SOFTWARE \ Allen
" Name " ,
Null,
Null,
Lpdata,
& Databuflen) = Error_success)
{
// Convert_01:
Char * Data = New Char [Databuflen];
Data = ( Char * ) Lpdata;
STD: cout " User name: " Data STD: Endl;
// Convert_02:
// DWORD * Data = (DWORD *) lpdata;
// STD: cout
}
If (Regqueryvalueex (hkey,
" ID " ,
Null,
Null,
Lpdata,
& Databuflen) = Error_success)
{
// Convert_03:
DWORD * Data = (DWORD * ) Lpdata;
STD: cout " User ID: " *DataSTD: Endl;
//Convert_04:
//Char * Data = new char [databuflen];
//Char * Data = (char *) lpdata;
//STD: cout
}
Note that this code has two conversions. What if we replace convert_01 and convert_03 with convert_02 and convert_04 respectively? The compiler won't embarrass you, but the running effect can be imagined.
Looking back at these two examples, what conclusions can we draw? As the title of this article --
I am only responsible for conversion!
When you forcibly convert-1 to the _ jb_prog_mode type and assign a value to the e_prog_index variable, you can only say that the e_prog_index variable does not contain the predefined values of _ jb_prog_mode enumeration, it cannot be said that the value of the e_prog_index variable is invalid (in fact, there is a deep relationship between the enumerated type and the integer type ). The compiler will not impede your transformation, and whether the conversion is meaningful is another matter. Of course, you should take some measures to check whether the enumerated variables contain predefined values. Otherwise, you may receive surprises from time to time.
In C/C ++, enumeration is actually a form of Integer constants, which is essentially just an aidProgramIdentifies a group of related Integer constants. In terms of semantics,
Enum _ Jb_prog_mode
{
Program_one = 0,
Program_two
} ;
Equivalent
Static Const Int Program_one = 0;
Static Const Int Program_two = 1;
C/C ++ gives programmers the greatest freedom to choose, which also reflects C/C ++'s trust in programmers. However, whether such trust helps us or hurts us depends on the practices of programmers.
Finally, I reference a passage from Robert B. Murray [1] to end this article:
It is not good to write a program that relies on vague and subtle rules defined in the language, even if the author understands its meaning and ensures that it can run correctly, the next person who maintains this Code may not be able to do this. A good practice is to stick to the widely used and understood parts of the language to write programs.
See also:
- Allen Lee; FAQ on enumeration [C #, Il, Bcl]
- Allen Lee; I don't care! [C #]
- [1] [us] by Robert B. Murray; translated by Wang Xin; C ++ programming idioms-common methods and skills for senior programmers; China Power Press, 2004