C ++ learning points

zhaozj2021-02-17  62

This is an article I saw on the Antibody Programming Network (http://www.5xsoft.com).

It's not bad, it's posting it to everyone.

1. When the pointer is passed, we can modify the content pointed to by the pointer. However, if you want to modify the object pointing to the external pointer is not possible. For example, the external pointer to the function is transmitted to allocate space, and the pointer to the pointer or pointer must be passed. 2. CHAR Carry [10] = {0}; the compiler will place all things thereafter 0; 3. When the function returns a value, the returned thing is paid to a type of the same name, it cannot be left value. ; 4. Const * i; int * const * i; int * const i; the first two functions are the same, indicating that the content points to the i is unchanged; the last description pointer points to the address constant, but the content varies. 5. Const member functions in the class. Defined as a CONST after prototype. Constant functions cannot modify any properties in the class. But there are two ways to modify. a) {(myclass *) THIS-> MEMBER1 = VALUES;} b) Define a member to Mutable by a constant function to modify. 6. The constant const type in the class cannot be used in the class to define an array. ENUM {One = 100; TWO = 2}; defined one, TWO can be. Usually, an ENUM definition problem: enum a {l = 9, z}; the value of z is 10.7. The intment definition with const can be used to open an array, but the elements in the constant array of constings cannot be Used to define an array. 8. Calculate the space of the variable with the sizeOf, if it is an array, press the actual space to return; constant strings (actually the variable opened in a static memory area) SIZEOF returns to the actual length plus one. If it is a pointer, it is not considered to point to the space size, and only returns the size of the pointer type. If you use the SizeOf to calculate the row ginseng, even if the group is also returns a size of a related type pointer. 9. Shaped like int aRray [] = {12, 124, 433}; the compiler will automatically assign the length of the three elements to IARRAY. The number of element lengths is calculated as a sizeof (IARRAY) / SIZEOF (* IARRAY). 10. Copy Construction Function: When the collections and the actual parameters are combined, if the transmitted value of the complex object is called, the calling constructor generates a temporary object as an argument, and the temporary object is sent to the destructor is released. When the return value is a complex object, it is also a call copy constructor to assign a value. This will appear in cases where the constructor and the destructive function are not equal. The prototype of the copy constructor is A (A &), we can overload in the class. (The default copy constructor is a bit (bit) copy method: shallow copy, no copy of the pointer points to the content). 11. Volithile type variable tells the compiler, this variable does not need to optimize code optimization. In multi-threaded applications, if we read a variable to the register, at this time, the time film expires, dealing with other threads, when re-obtaining the process, the Volatile type tells the process, and read data from the variable to the register. Instead of direct processing with register data, it can prevent dirty data. 12. Class and struct have the same function to some extent, but only the former default members are private, the latter is common in the default member. Therefore, Class is not a reserved word 13. C and C compiler required for C , which is different after compiling the same function name, so it is necessary to use Extern "C" to tell the compiler when referenced by reference to the library files referenced. C function, compile according to the rules of C. Usually we use the standard header files have been processed.

14. #include "filename"; #include , the former is first looking for files in the current directory. If you can't find it to find it to the system specified, the latter is directly to the path specified by the system. 15. The static variables allocated anywhere, the lifecycle and the main process is the same. The second time defines an existing STATIC variable, which has no effect on the variable, but its visible range is only within the defined range. (The postgraduate has been made!) (It is not difficult to understand from the characteristics of static variables, and the Static type in the class is shared by all objects) 16. Inline function (inline) is similar to the actual and macro, the place in the inner function Expand the function to avoid the outlook in the function call, such as stack, improve efficiency. But the price of the inline function is: the code is increased. The inline function is suitable for member functions and free functions. The function implemented in the class is automatically inlined function. Inline must be defined to the implementation of functions, for example: Inline int plusone (int) is invalid. The friend function is automatically changed to the inline function in the body of the class. 17. #include #define debug (x) cout << # x "=" << x << Endl #x represents x is used as a string output. 18. Assert (0! = 0); if the conditions in Assert are false, return the program during the run, and report the line number of the error code. (#Include ) 19. Static object calls itself when the main or exit () is called. This means that in order to call exit () in an object's destructor, it is possible to enter a dead cycle. Calling Abort () to exit the function, the destructor of the static object is not called. We can use ATEXIT () to specify the operation you want to perform when you jump out of the MAIN or call EXIT, and use the ATEXIT registration function to call the destructor's destructor. Void exit_fn2 (void) {printf ("exit function # 2 called / n");} // Handling function ATEXIT (exit_fn2); 20. The global variable actually uses static storage. The structure of the static variable is called before entering Main, and it calls its destructor. The name of the variable is made of small range (C ): //*.cppint a; // static variable, but for extern int a; 即 is global, external visible static int b; // static variable, static and In contrast, only in * .cpp is valid, it is invisible to other units (files). The definition of the function is the same.

The static member variable of Main () {} class can be assigned: INT x :: S = 23; (in * .cpp, no matter how private private privacy) 21. Namespace: Define a name space, then use unsing You can convert the current type context to the name space .Namespace Math {ENUM SIGN {Positive, Negative}; Class Integer {Int i; Sign S; Public: Interger (int i = 0): i (i) {... ...} SIGN SIGN () {.........} ......................}; // end ClassInterger A, B, C; Interger Divide (Interger);} // no; void q () {Using Namespace Math; Interger A; // Hides Math :: aa.sign (negative); Math :: A.SIGN (Positive);} 22. General for Functions Flaot F (Int A, INT B); Some C After compiler, generate _f_int_int's name, some C compiler generates _f's name. Therefore, in the library function of the C , use externaln "c" to tell the compiler, press the rule to compile functions. Similar to extern "c" {# include "myhead.h"}, C also supports Extern "C " {}. 23. When the function is called, the pass reference is also a pointer stack. 24. Constructing function, destructuring function, assignment constructor, overloaded =, four call sequences: (three functions have been implemented) a) x x; x a = x; Result: x: Construct x: Copy_structb X x; x a; a = x; result: x: constructx: constructX: COPY_STRUOPERATOR = x: Destructure If there is no assignment function result: x: constructx: constructoperator = x: destruct (if directly x a = x; Do not drop with the general constructor, call the copy constructor) Pointer to the member function to the class: set int x :: A (void) {} ​​x x; int (x :: * pf) (void) = & x :: a; (x. * pf) (); pointer to a member variable: set int i; is a member variable INT x :: * pm = & x :: i; x: x: x: x:

x. * pm = 12; x * p = & x; p -> * pm = 11; 25. operator overload const x & operator () // b; const x operator (int) // b ) The second parameter is dumb, never use. 26. Automatic type conversion a.) Class One {b} class two {public: one () {} public: two (const one "; void f (two; f)}} main () {on} (ONE);} At this time, a constructor in TWO is called for automatic conversion of the type. But the efficiency is not high. You can block an implicit type conversion. The method is as follows: Two TWO is given to class two {public: explicit two (const one &) {}}; ExPlicit only works on constructor. The function must be called this at this time: F (two); 27. An ideal String class, it knows how to convert from String to Char *: Class string {private: char * s; public: string (const char * s = ") {s = new char [strlen (s) 1] Strcpy (s, s);} ~ string () {delete s;} operator const char * () const {return s;}}; int main (void) {String str1 ("lizhihui2"); string str2 (" Lizhihui2 "); strcmp (str1, str2);} 28. If there is a variety of conversion methods from one type to another clock, it will be erroneous: classss y; class x {public: operator y () const; // convert x to y}; class y {public: y (x ); // converT x to y}; void f (y); main () {x x; f (x); // error: Ambiguous converness} 29. Delete array object: foo * fp = new foo [100]; delete [] fp; or delete [100] fp; make pointer more like array: int * const Q = new int [10]; this Q cannot be moved more like Array.

30. NEW 堆 memory function void out_of_memory () {Printf ("out of memory! / n"); exit (1);} main () {set_new_handler (out_of_memory); ...............} 31. NEW and DELETE a global overload method void * operator new {printf ("Operator new:% D Bytes / N", SZ); void * m = malloc (SZ); if (! m) PUTS ("OUT of Memory / N"); Return M;} Void Operator Delete (Void * M) {PUTS ("Operator Delete./N"); Free (m);} Class S {Int i [100]; public : S () {PUTS ("S :: S ()");} ~ s () {PUTS ("s :: ~ s ()");}}; int main (int Argc, char * argv [] ) {Int * p = new int (23); // Operator new: 4 bytes delete p; // operator delete. S * pp = new s; // operator new: 400 bytes delete pp; // Operator delete. S * PA = new s [3]; // Operator new: 1208 bytes, more 8 bytes for array info delete [] pa; // Operator delete. (C bilder unsupport) Return 0;} Default system New and DELETE is to call Malloc and Free, and the system should maintain a memory allocation table. The assigned memory should remember its size and start addresses, and release the address based on the start of the address. 32. Overloading NEW allocates space Class S {INT I; PUBLIC: S (INT IX = 0) {PUTS ("s :: ~ s ()");} Void * Operator new (size_t d, void * loc) {return loc;}}; int main (int Argc, char * argv [in [10]; s * ps = new (len 1) s 3412); // The first function is equivalent to telling the NEW where to start allocating space (implicit); // It is the length to be assigned. Special assignments should be taken to take special release. Return 0;} New allocates the space to the S object on the space of the LEN.

(New heavy load first parameters must be automatically transmitted to it to a size size) 33 for the SIZE_T system. Jump function setjmp, longjmpvoid oz () {Printf ("there 's no placelike home / n"); longjmp (kansas, 47);} jmp_buf kansas; int main (int Argc, char * argv []) {if ( SetJMP (Kansas) == 0) oz (); Else Printf ("i Have A Dream ../ n"); Return 0;} // After performing oz (), you will immediately jump to Printf ("i Have A DREAM ../ n "); Execute SetJMP () is a special function, when called, it is put in the current process status in BUFF and returns 0; if you use longjmp to the same BUFF Operation, this is like returning from SetJMP again, that is, the back end of SetJMP correctly pops up. At this time, the return value is the second parameter for longjmp, so it can be found that it is actually returned from longjum. 34. Abnormally customized and throw Class Up {}; Class Fit {}; void g (); Void F (INT i) throw (Up, FIT) {Switch (i) {casse 1: throw up (); case 2: throw Fit ();

转载请注明原文地址:https://www.9cbs.com/read-29325.html

New Post(0)