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
// Www.dgbalance.com
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. Special Data Types in c ++
String Length: string: size_type
Number of elements in vector: size_type
Number of elements in the array: size_t
Distance Measurement between two pointers: ptrdiff_t
5. Multi-dimensional array
Multi-dimensional arrays are actually arrays in arrays. For example:
Int ia [3] [4]; // arry of size3, eachelement is an arry of ints of size 4
Int (* ip) [4] = ia; // ip points to an arry of 4 ints
Int * ip [4]; // arry of pointer to int
6. Efficiency of p ++ and ++ p in c ++
We generally use the prefix operator in c ++. Because the prefix operation is more efficient, we only need to add 1 and then return the structure with 1. the Postfix operator must first save the original value of the operand, in this way, the value before 1 is returned as the result of the operation.
* Beg ++; equivalent to * beg; ++ beg;
7. function parameter problems
Const parameters can avoid copying
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 shape parameter, and not 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. function pointer
Returns the pointer to the function. For example: int (* ff (int) (int *, int). The explanation is as follows: ff is a function with the int type, the function returns the pointer to the function. The Pointer Points to the int (*) (int *, int) type. That is, the function returns the int type with two parameters. The typedef definition is more concise and easy to understand:
Typedef int (* PF) (int *, int );
PF ff (int );