Review C + + to quote section, do the following collation. 1. Const
Used to modify variables to indicate that they cannot be modified. Often used to modify function parameters and ordinary variables. The frequently asked points are pointer constants and constant pointers.
Pointer constants: int * const p; Indicates that the pointer p is a constant, pointing to an int row variable.
int A;
int b;
int * Const P = &a;
p = &b; Pointer p cannot be modified
Constant pointer: const int *P; Indicates that P points to a constant of type int.
int a = 1;
const int b = 2;
const int *p = &a;
p = &b;
*p = 2; Wrong, the content that P points to cannot be modified
Remember, the constant pointer is the first constant (const) pointer (*), and the pointer constant is the opposite. 2. Const usage
To indicate an immutable modifier function parameter
You can use the const modifier when you want the function argument to not change inside the function. Not necessary for basic data type int, etc., available to pointers and classes.
void Fun (const char *pstr);
void Fun (a);
void Fun (const A &a); /* When you want object A to be immutable, you can use the const reference of object A to reduce the copy, copy, and destructor of the temporary object. */
Modifying Variables
A common usage that modifies variables that cannot be changed. modifier function return value
You want the function return value to be immutable, such as the C_STR usage of string.
const char * C_STR (std::string str);
modifier function Definition body
Any function that does not modify the data member should be declared as a const type. If you accidentally modify a data member while writing a const member function, or call another non-const member function, the compiler will point out the error, which will undoubtedly improve the robustness of the program.
Class Stack
{public
:
void Push (int elem);
int Pop (void);
int GetCount (void) const; Const member function
private:
int m_num;
int m_data[100];
3. Reference
When I began to learn, I said the reference is an alias, the personal feeling too general, look up the relevant information (the nature of the reference in C + +) and verify that the reference is a pointer, there is address space, the internal storage of other variables address.
Examples are as follows: from the Assembly language, the two are essentially the same.
int i=5;
00a013de mov dword ptr [i],5 //Add literal constant 5 to variable i
int &ri=i;
00a013e5 Lea Eax,[i] //The address of the variable i is fed to the register EAX
00a013e8 mov dword ptr [Ri],eax // The contents of the register (i.e. the address of the variable i) are fed into the variable ri
ri=8;
00a013eb mov eax,dword ptr [ri] //Send value of variable RI to register eax
00a013ee mov dword ptr [eax],8 //Send the value 8 to the cell with the address of the EAX content return
0;
00a013f4 xor eax,eax
Here are some ideas to mention in passing. A variable is just an abstract concept given by the compiler, and actually only address and address space content. 4. Const Reference and non-const reference
The difference is that a const reference can be bound to a similar type and to a right value. But not modifiable. Cases:
Double d = 12.12;
const int & a = D;
const int & b = 12;
Non-const references cannot bind right values and similar types, and can only be bound to the same type. Cases:
Double d = 12.12;
int & a = 1; Wrong, cannot bind right value
int & b = d;//wrong, cannot bind non identical type of variable
int aa = 1;
int & c = AA; Right
Why so, the specific reasons can be seen in C + + design and evolution, in the c++2.0 to make this provision.