Const usage summary, const usage
1. const modifier variable
Int B = 100; const int * a = & B; // 1int const * a = & B; // 2int * const a = & B; // case 3 const int * const a = & B; // case 4
There are three cases of const modification:
First: const is on the left of "*", and const is used to modify the variable pointed to by the pointer, that is, the pointer points to a constant, such as Case 1, Case 2;
2. If const is on the right side of "*", const is used to modify the pointer itself, that is, the pointer itself is a constant, as shown in case 3;
Third: If const is on the "*" side, const modifies both the pointer itself and the variables referred to by the pointer, as shown in case 4;
Note: the relative position of const is only related to "*" and has no relationship with the type declaration of the variable. Second, const can not initialize the variable indicated by the pointer when it modifies the variable, however, const must be initialized when modifying the pointer itself.
2. Use 2.1 const to modify the function parameters in the function.
If the input parameter uses "pointer transmission", adding const can prevent accidental changes to the memory unit pointed to by the pointer and play a protective role, such as the StringCopy function.
// Input parameter: strSrc output parameter: strDestvoid StringCopy (char * strDest, const char * strSrc );
If you want to protect the pointer itself, you can declare the pointer itself as a constant. For example:
void OutputString(const char* const pStr);
If a parameter is used for output, no matter what type it is, no matter whether it uses "pointer transfer" or "reference transfer", it cannot be modified by const, that is, const can only modify the input parameter. In addition, if the input parameter uses "value transfer", because the function will automatically use the real parameter to copy the initialization parameter, even if the parameter is modified inside the function, the change is only a copy of the stack rather than a real parameter. Therefore, you do not need to modify the const. It is recommended that you do not change void Func (const int x) to void Func (const int & x), which is confusing for the purpose of improving efficiency. Because the parameters of the basic data type do not have a structure or structure process, we recommend that you use "const & pass" unless the basic data type parameters are ".
2.2 const modifier function return value
If a const modifier is added to the return value of the function passed by pointer, the return value is a contractual constant and cannot be directly modified, and the returned value can only be assigned to the same type pointer with const modifier (unless forced conversion), for example:
Const char * GetString (void); // function declaration char * str = GetString (); // compilation error const char * str = GetString (); // correct
If the return value of a function uses the "value transfer" method, it is meaningless to add the const modifier because the function will copy the return value to an external temporary storage unit.
2.3 const member functions
Class CTextBlock {public :... std: size_t length () const; private: char * pText; std: size_t textLength; bool lengthIsValid;}; std: size_t CTextBlock: length () const {if (! LengthIsValid) {textLength = std: strlen (pText); // The const member function cannot assign value to the lengthIsValid = true; // value to the member variable} return textlength ;}
Const member functions cannot assign values to member variables in the class. If you have to assign values to member variables in the class, you can use mutable to modify member variables, such:
mutable std::size_t textLength;mutable bool lengthIsValid;
3. Comparison between const and # define
First, const constants have data types, while macro constants do not. The compiler can perform type security checks on the former, but only character replacement on the latter. There is no security type check, and unexpected errors (marginal effect) may occur during replacement ).
2. During macro definition compilation, all macro definitions are replaced with actual values during pre-compilation, and then compiled. The const value is a constant during compilation, at the same time, const constants may produce less code than define.
Third: Some integrated debugging tools can debug const constants, but cannot debug macro constants.
Fourth: # define cannot create a class exclusive constant or provide any encapsulation. const can define class exclusive constants, such:
class GamePlayer{private: static const int NumTurns = 5; int scores[NumTurns];}
Suggestion: for simple constants, it is best to replace # define with the const object or enums; corresponding to the macro like a function, it is best to replace # define with the inline function.
Additional knowledge:
Const in C is "a common variable that cannot be changed". It always occupies memory and is a global variable. The C compiler cannot regard const as a constant during a variation, the function in C is unreasonable.
const bufsize = 100;char buf[bufsize];
In C ++, const is an external connection by default, and const is an internal connection by default.