the basic C + + Foundation of the Piglet's attack into the Doctor (II.)
Const ,auto, decltype
In the previous chapter we introduce some common types and common problems, and then introduce some of the things that are not particularly useful in practical projects when studying.
One, constant const
We want to define a variable that cannot change its value, and we add the qualifier Constto the type of the face.
A const object must be initialized, and it cannot be changed once it has been created, so the const variable can only appear to the right of the equal sign.
c11 Span style= "font-family: ' Times New Roman ';" >constexpr constexpr const constexpr
Second, Processing Type
When our program becomes more and more complex, there are two kinds of problems, the first is the type of writing is more troublesome, such as unsigned char * This, you may have to write many times, the second is that the program is written long you can not understand what the type of.
The way to solve the first problem is to do the "type alias", that is, the individual name. For example, just can be made:
typedef unsigned char
that unsigned char > now.
c11 Standard has a auto auto i=1; This time you know i
There is another kind called Decltype An indicator of type decltype (f ()) sum=x; the type of sum is the type of f () . These two new methods here not much to say, can look at the book, more than a few times there is no problem, of course, no matter what trouble seems to have no catastrophic problem.
Third, Custom Data Structures
We can declare a struct type when we want to define a data type and do not want to add an arithmetic function . Here is a simple example, note that this curly brace is good at the back.
struct point{
int x;
int y;
};
struct Point p;
In addition, we may compile a header file ourselves, some things can only be declared once, so we need to use a kind of called header file protector. is to compile the time to see if the header file has already been defined, if the definition is not repeated. This is useful, you can write this sentence in each of the header files:
#ifndef Point_h
#define Point_h
struct point{
int x;
int y;
};
/* Header File Contents */
#endif
C + + Basic (ii) constants, processing types, custom header files for pigs