C ++ FAQ
1. c ++ variable initialization rules
Whether the built-in type variables are initialized depends on the definition location of the variables. All variables defined in the function in vitro are automatically initialized to 0, and all variables defined in the function body are not automatically initialized. Class type variable initialization. A Class controls class initialization of objects by defining one or more constructors. If Initialization is not provided when a class variable is defined, this class is implemented through the default constructor.
2. Reference and pointer)
A reference is another name of an object. It is defined by adding an & symbol before the variable. Initialization is required during definition:
Int ivals = 1024;
Int & refval = ival; // OK: refval refers to ival
Int & refval; // error: A reference must be initialized
Int & refval = 1024; // error: initializer must be an object
Operations acting on references are exactly the same as operations acting on objects. After a reference is initiated, the referenced object is bound to the object pointed to during initialization as long as the reference is changed.
A const reference is a reference to a const object. It can be read but cannot be used to modify the object it points to. A common reference cannot point to a const object.
A pointer is used to point to an object. Its value is the address of the object to be directed to, and is defined by adding * Before the variable:
String * pstr, ppstr; // pstr is a pointer to string, ppstr is a string
When using a pointer to operate on memory, it is prone to out-of-bounds errors. We recommend that you initialize the pointer immediately after it is defined. Only the following four types of values can be used to initialize or assign values to the pointer: 0 constant expressions; the address of the type matching object; the next address of the same object; another valid pointer of the same type. C ++ provides a special pointer void *, which can save the address of any type of object.
3. pointer and const qualifier
A pointer to a const object. The definition does not require initialization. You can modify the pointer to ask questions but cannot modify the object. The const object must be pointed to using the const type pointer.
Const double Pi = 3.1415926;
Const double * PTR = & PI; // PTR is a pointer to a const
The const pointer is a constant and its value cannot be modified. It must be initialized during definition. For example:
Double Pi = 3.1415926;
Double * const PRT = & PI; // PTR is a const pointer
The value of a const pointer to a const object cannot be changed. You can only access the indicated object and cannot modify the value of the object. Eg:
Const double Pi = 3.1415926;
Const double * const PTR = & PI; // PTR is a const pointer to a const object
4. pointer and typedef
Use an example to illustrate this problem. For example:
Typedef string * pstring;
Const pstring CSTR;
Most people think that CSTR is a const string * (the original formula is equivalent to const string * CSTR) type, but this is incorrect because typedef is considered as a text extension. When declaring const pstring, const modifies the pstring type, which is a pointer. Therefore, it should be understood that CSTR is defined as a const pointer pointing to the string type, which is equivalent: string * const CSTR;
5. The length of a special data type string in C ++: number of elements in the string: size_type array vector: number of elements in the size_type array: size_t distance measurement between two pointers: ptrdiff_t5 and multi-dimensional arrays are actually arrays in the array. For example: int Ia [3] [4]; // arry of size3, eachelement is an arry of ints of size 4int (* IP) [4] = IA; // ip points to an arry of 4 intsint * IP [4]; // arry of pointer to int 6, P ++ and ++ P in C ++ are efficient. We generally use the pre-operator in C ++ because the pre-operation is more efficient, you only need to add 1 and then return the structure of adding 1. The post operator must first save the original value of the operand to return the value before adding 1 as the result of the operation. * Beg ++; equivalent to * beg; ++ beg; 7. function parameter const can avoid the copy operation. When an array is used as a function parameter, the compiler only checks whether the real parameter is a pointer, whether the pointer type matches the parameter without checking the array length. When an array is passed, the real parameter is a pointer to the first element of the array, and the shape parameter copies the value of this pointer. When an array is passed by reference, the real parameter must be exactly the same as the parameter type size. Two-dimensional array parameters: void printarray (INT (ARR *) [10], int rowsize); 8. The function pointer returns the pointer to the function. For example: INT (* ff (INT) (int *, INT); explain as follows: FF is a function with an int type. This function returns a pointer to the function, the Pointer Points to the INT (*) (int *, INT) function. That is, the pointing function returns the int type with two parameters. Typedef is more concise and easy to understand: typedef int (* PF) (int *, INT); pf ff (INT );