1. New and delete
Int * Pi = new int (0); // initialize the value to 0a * pA = new A (); // For the custom type, create an object in two steps, the first step is to allocate memory. The second step is to call the constructor. Pa-> function (); Delete Pa; // For custom types, call the destructor in the first step and release the memory in the second step. Int * Pi = new int [10]; Delete [] PI; // apply for a one-dimensional array of 10 elements int ** Pi = new int [5] [6]; delete [] [] PI; // apply for a two-dimensional array. Const int * PCI = new const int (1024 );
The const object created dynamically must be initialized at the time of creation and its value cannot be modified once initialized. After the Delete P operation, the pointer becomes a suspension pointer, which may point to any location in the memory. Once the object pointed to by the pointer is deleted, the pointer is immediately set to 0, that is, to null, which clearly indicates that the pointer no longer points to any object.
2. malloc and free
Error-prone dynamic memory management
1. Failed to delete the pointer pointing to the dynamically allocated memory, so the block of memory cannot be returned to the free storage zone. The failure to delete the dynamic allocated memory is called "Memory leakage (Memory Leak )". Memory leakage is hard to find. Generally, it is revealed only when the application runs for a period of time and consumes all the memory space. Check after the delete operation.
2. Read and Write deleted objects. If the pointer is set to 0 after the object pointed to by the pointer is deleted, it is easier to detect such errors.
3. Use two Delete expressions for the same memory space. An error occurs when two pointers direct to the same dynamically created object. If you perform the delete operation on one of the pointers, return the memory space of the object to the free storage zone, and then delete the second pointer. In this case, the free storage zone may be damaged.
3. Const usage
1. The left value after the definition cannot be modified, so the definition must be initialized.
The const object is a local variable by default. To use it globally, the Declaration extern must be displayed.
File 1 extern const int buffersize = 512; // define
File 2 extern const int buffersize; // use
2. a const reference is a reference to a const object.
Const int ival = 1024; const Int & refval = ival; Int & ref2 = ival; // n non_constant reference points to a constant object. The const reference can be initialized to a different type of object or initialized to the right value int I = 42; const Int & R = 42; // legal for const references onlyconst Int & r2 = R + I; double dval = 3.14; const Int & rI = dval;
// The Compiler converts the code to an encoding in the following format: int temp = dval; // create temporary int from the doubleconst Int & rI = temp; // bind Ri to that temporary
If Ri is not a const, you can assign a new value to ri. In this way, dval is not modified, but temp is modified. The programmer who expects that the value of Ri will modify dval will find that dval has not been modified. This problem is completely avoided by allowing the const reference to be bound to a value that requires temporary use, because the const reference is read-only.
3. Constant pointer and pointer constant
Const int * P1 = & I; // constant pointer, pointing to a constant pointer, equivalent to int const * P1 = & I; // * P1 = 2; // The content pointed to by the pointer cannot be changed to p1 = & J; // the pointer can be modified. It can be changed to another variable int * const P2 = & I; // the pointer constant and cannot be changed to P2. Must be initialized. A pointer is a constant * P2 = 2; // The content pointed to by the pointer can be modified. // P2 = & J; // The pointer cannot point to another variable const int * const P3 = & I; // The constant pointer to the constant const Int & rI = I; // constant reference, the RI value cannot be modified. // RI = 10; I = 10; OK
4. Common functions
// When writing a class, declare as many common functions as possible. To meet the call needs of constant objects.
Int getn () const // a regular function. The data member of the class cannot be modified in the function.
// The Compiler interpreted it as int getn (const A * This)
// It must be a member function of a class. A common object can only call a common function.
Const modifies the parameters to improve the efficiency of parameter transfer and ensure that the input parameters are not modified! Generally, do not return the address or reference of the data member to avoid damaging the encapsulation of the class.
Const string & gets () const {return s;} // use const to modify the return value
4. Static
Int I; // instance Member, which belongs to an object. Each object has its own copy and can only be accessed through an object.
Static Int J; // static member, which belongs to a class. All objects share one copy of data. It can be accessed through the object name or class name.
Int A: K = 0; // The static member must be initialized outside the class.
Static void function (); // static member function: No this pointer. It cannot be an instance Member of the category, but only a static member of the category.
5. Initialization
Int ival (1024); // The initialization method is put in brackets for direct initialization. [more flexible and more efficient]
Int ival = 1024; // copy the initialization syntax using the equal sign (=)
Initialization list
A: A (): I (2), J (3), K (10 ){......} a: A (int A, int B): I (a), J (B) {} // A's private data member I, j
6. Class
1. m_id, m_cname, Private member of the constructor student
Student::Student(char *pName, int ssId){m_ID = ssId;strcpy(m_cName, pName);cout << "construct new student" <<endl;}
2. copy constructors
Student::Student(const Student& Stu){m_ID = Stu.m_ID;strcpy(m_cName, Stu.m_cName);cout << "construct copy of" << Stu.m_cName << endl;}
3. You do not need to define the shortest copy function. c ++ uses the default copy constructor for deep copy.
Student::Student(Student& stu){cout <<"Constructing " <<stu.pName <<endl;pName=new char[strlen(stu.pName)+1];if(pName!=0){strcpy(pName, stu.pName);}}
7. Domain Name Space
Namespace N1 {void F () {}} // custom useing namespace N1; F ();/call
Or N1: F ();
8. function parameter default
Int add (int x, int y = 5, int Z = 4); y int add (INT x = 3, int y, int Z); n
Default Value
9. Pass the reference pointing to the pointer
Void ptrswap (int * & V1, int * & V2) // int * & V1 indicates that V1 is a reference and is associated with a pointer to an int object. {Int * TMP = V2; // that is, V1 is the alias of any pointer passed into the ptrswap function. V2 = V1; V1 = TMP;} // use the pointer ptrswap (Pi1, Pi2) when using)
10. header files
1. the header file is used for declaration rather than definition.
2. Define a const object
3. inline functions
11. Static Association and Dynamic Association
Calss A {public: void F () {cout <"A" ;}}; Class B: Public A {public: void F () {cout <"B" ;}}; Class C: Public A {public: void F () {cout <"C" ;}}; Class D: public A {}; // the function that inherits the parent class is an abstract class and cannot be instantiated. (This example is dynamically associated) void test (A & RA) {Ra. F (); // depends on the reference of calling F1 or the declared type of the pointer. It is called static concatenation, that is, the function body to be executed has been determined during compilation .} Void main () {B; A & RA = B; RA. F (); // output AA * pA = & B; pa-> F (); // output atest (B ); // output a // If virtual is added to the front of Class A, it becomes Dynamic Association, and B is output at this time ;}
12. Dynamic Association and editing Conditions: 1. Use the reference (pointer) of the base class to point to the object of the derived class 2. The dynamic association effect can be achieved only when virtual functions are called.
Class A // abstract class: class containing pure virtual functions. It cannot be instantiated. {Public: Virtual void F1 () {cout <"a F1" <Endl ;}// declare pure virtual function in the parent class
};
If a class may be used as a base class, you need to write its destructor as a virtual function. The destructor of a derived class automatically becomes a virtual function.
13. pointer
1. initialize the pointer before use !!!!
----> If an uninitialized pointer is used for running errors, the undefined values in the pointer are considered as addresses, and the bit content stored in the memory address is manipulated. Using Uninitialized pointers is equivalent to manipulating the basic data stored in this uncertain address. Therefore, resolving Uninitialized pointers usually causes program crash.
2. Special pointer type void *, which can save the address of any type of object
3. pointer to pointer
// Definition: int * Pi = & I; int ** PPI = π
Use: unreference twice
cout<<**ppi<<endl;
4. pointers and arrays:
Int * IP = IA; // ip pointing to IA [0] IA is an array name. Int * ip2 = IP + 4; // OK: pointer + 4, then move 4 units until backward, and then assign the value to pointer ip2. Be sure not to cross-border
The pointer also supports the subtraction operation.
Int last = * (IA + 4) // obtain the fourth element of the array, and assign the value to lastint last = * Ia + 4 // obtain the first element of the array, + 4 after unreferencing
Pointer and subscript
Int * P = & Ia [2]; // OK: P points to the 2nd elements of the array IA, Int J = P [1]; // OK: P [1] is equivalent to * (p + 1), that is, Ia [3] int K = P [-2]; // OK: P [-2] is equivalent to IA [0]
Pointer traversal array ------- pointer is equivalent to the array iterator
Const size_t arr_sz = 5; int int_arr [arr_sz] = {0, 1, 2, 3, 4}; // pbegin points to the first element of the array, pend points to the last element for (int * pbegin = int_arr, * pend = int_arr + arr_sz; pbegin! = Pend; ++ pbegin) cout <* pbegin <''; // output the current object
14. bitwise operators
Unsigned char bit1 = 0227 // 10010111 ~ Bitwise NOT (bitwise inversion) bit2 = ~ Bit1; // 01101000 <left shift (left shift) bit1 <1 // 00110110 left shift, right shift (right shift) bit1> 2 // 00100101 shifted to the right, and 0 & bitwise AND (bit and) and bitwise get and operation on the left ^ bitwise XOR (bit-by-bit or) bitwise OR bitwise/bitwise
15. Auto-incrementing auto-subtraction Operator
Rear ++ A ++;
Cout <* P ++ <Endl; equivalent to cout <* P <Endl; ++ P;
16. Use Preprocessing for debugging
int main(){ #ifndef NDEBUG cerr << "starting main" << endl; #endif}
Int main () {# ifndef ndebug cerr <"starting main" <Endl; # endif} If ndebug is not defined, the program writes the information to cerr (output to the console). If ndebug has been defined, the code between # ifndef and # endif will be skipped during program execution.