In the original C language, enum, const, auto and other keywords were not perfect, did not play their due functions, they are second-class citizens, but as time goes on, in C++ they have been perfected, some of them have been given new functions, played an important role. On the contrary, first-class citizens who used to be the king of the group, such as macro, became pariahs in C++ and were abandoned. Let’s take a closer look at the various scenarios.
1, enum
This keyword can be used to define so-called enumerated types, the natural meaning of enumeration is a limited scope of integer, such as color rules for red, orange, yellow, green, blue, violet seven color, so you can use the Numbers 0 to 6 to refer to, such as process state regulations for sleep again, running, pause, death, etc., can be used respectively 0 to 3 Numbers to refer to, in c + +, You can do this with the following code:
enum spectrum {red, orange, yellow, green, blue, cyan, purple};
enum spectrum color;
color = red; / / right
color = 1; / / error
Copy the code
This code demonstrates the use of enumerations in C++. The first line defines an enumeration of the spectrum type ranging from red to purple, where red defaults to 0 and subsequent enumerations are incremented by this value. In the second line, we use this type to define an enumeration variable color. The value of color can only be one of the seven colors, and not any other. Therefore, it is wrong to assign 1 to color directly.
2, the const
In the C language standard, the role of the const keyword is described as ambiguity of the meaning of “read-only variables”, use the const modified variable cannot assignment again, but the but again not considered constants (for example, can’t be used in a case statement total), therefore a const alone used to modify an ordinary variable, is almost extinct in the C language. Almost the only use of const in C is to modify a pointer, such as:
char *strncpy(char *dest, const char *src, size_t n);
Copy the code
The function of such a const is to restrict access to the pointer so that it can only be read and cannot be written, making the code safer.
In C++, the const keyword is given more useful functionality, removing ambiguities. Its functions are summarized as follows:
First, define constants with types that can be used instead of macros, such as:
const float PI = 3.1415;
Copy the code
The above definition statements are better than macro definitions because they have type detection. In contrast, macro definitions have no type detection and are no longer appreciated in C++ code.
Second, it is used to distinguish function characteristic marks. In C++, functions can be overloaded. If the function itself is const, or one of its arguments is const, the characteristic of the function changes and can be overloaded. For example, in each class A, we can define two overloaded member methods:
void A::f(a);
void A::f(a)const;
Copy the code
These two functions are considered overloads, where the const version of the function can only be called by an object that is const. For example:
A a; // A common object a is defined here
const A b; // Here we define a const object b
a.f(a);// Version A::f() is called
b.f(a);// The const version of A::f() is called
Copy the code
Third, to make the program more robust and efficient, we make all variables that do not need to be changed const. In short, we use const wherever possible. This is consistent with the C language. For example, in the strncpy function above, we use const because we don’t need to change the SRC pointer.
3, auto
In C, auto is almost obsolete because it means that local variables are stored on the stack, so called automatic variables (as opposed to static variables). The syntax of C language also stipulates that the default storage area of local variables is the stack, so auto is a chicken ribs, gradually abandoned.
int f(void)
{
int a;
auto int b;
static int c;
}
Copy the code
The above code is a complete illustration of why AUTO was abandoned in C. A is a local variable that is not decorated by any storage class and is stored in stack memory by default, which means its life cycle is temporary. B is exactly the same as a, and auto is their default storage class modifier. The variable C, on the other hand, is stored in a static data area, meaning that its lifetime is the same as that of the entire program.
In C++, auto is given the meaning of permission, which is more like its name: automatic. Auto in C++ means “automatic type derivation.” here’s an example:
int a;
auto b = a;
Copy the code
When defining b, if the type of B cannot be determined in advance, but can be automatically matched by the type of the assigned data, this is the actual use of auto, the above example is relatively simple, in some template classes, template function design, auto keyword can play a multiplier effect with half the effort.
4, the # define
In C, macros are the king of the Linux kernel code, but in C++ they are not as popular. Macros are fast, but because of other disadvantages, they are discarded in favor of const constants, inline functions, and so on. The disadvantages of macro definitions are:
First, you can’t determine the type. I mentioned const constants above. Undefined macros are not good for humans or machines.
Second, the logic of complex macros with parameters is difficult to understand, especially if the parameters are complex expressions.
In order to retain the advantages of macros (expansion directly in the code call, no jump), C++ invented inline functions. Inline functions themselves are functions, so there is no macro parameters without type, no syntax detection and other disadvantages. Inline functions also have the advantage of faster macro execution because they insert code directly into the call during compilation (rather than preprocessing), eliminating the need for a jump.
C++ has many more interesting features to explore. It is a super-language federation tool that is hard to learn and easy to use, but will eventually see the light of day.