Summary of C ++ learning points (II)

Source: Internet
Author: User

In the previous article, we have already introducedC ++The first half of the learning points summary is the C ++ learning points summary.) In this section, we will continue to introduce the second half:

16. The implementation of inline functions is similar to that of macros. When an inline function appears, expand the function to avoid the function calling from the stack, such as the stack, and improve the efficiency. But the cost of inline functions is: The Code increases. Inline functions are suitable for member functions and free functions. The functions implemented in the class are automatically inline functions. Inline must be defined on The Implementation of The function. For example, inline int PlusOne (int) is invalid. A friend function is automatically implemented as an inline function in the class body.

17. # include

 
 
  1. #define DEBUG(X) cout<<#X"="<  

# X indicates that X is output as a string.

18. assert (0! = 0); if the condition in assert is false, the program is exited during running and the row number of the error code is reported. (# Include)

19. The static object calls its own destructor only when main ends or exit () is called. This means that it is very dangerous to call exit () in the destructor of an object and may enter an endless loop. Call abort () to exit the function. The static object destructor will not be called. We can use atexit () to specify the operations to be performed when jumping out of main or calling exit. Functions registered with atexit can be called before the destructor of all objects.

 
 
  1. Void exit_fn2 (void)
  2. {
  3. Printf ("Exit function #2 called \ n ");
  4. } // Processing functions
  5. Atexit (exit_fn2 );

20. global variables actually use static storage. The construction of static variables is called before entering main, and its destructor is called at the end of main. Variable names are defined in a small scope (c ++ ):

 
 
  1. // *. Cpp
  2. Int a; // static variable, but extern int a; that is, it is global and externally visible
  3. Static int B; // static variable. The opposite is static and extern, which is only valid in *. cpp and invisible to other units (files. The function definition is the same as above.
  4. Main ()
  5. {}

The static member variables of the class can be assigned the following values: int X: s = 23; (in *. cpp, both public and private can be used)

21. namespace: Define a namespace, and then use unsing to convert the current type context to the location specified by the namespace.

 
 
  1. namespace math  
  2. {  
  3. enum sign{positive, negative};  
  4. class integer{  
  5. int i;  
  6. sign s;  
  7. public:  
  8. interger(int I=0): i(i) {………}  
  9. sign Sign() {………}  
  10. …………………..  
  11. };//end class  
  12. interger A, B, C;  
  13. interger divide(interger, interger);  
  14. }//no ;  
  15. void q()  
  16. {  
  17. using namespace math;  
  18. interger A; //hides math::A  
  19. A.Sign(negative);  
  20. Math::A.Sign(positive);  

22. Generally, for the flaot f (int a, int B) function, some c ++ compilers generate the name of _ f_int_int after compilation, and some c compilers generate the name of _ f. Therefore, when linking c library functions in c ++, use extern "C" to tell the compiler to compile functions according to the c Rules. Similar to extern "C" {# include "myhead. h"}, c ++ also supports extern "C ++ "{}.

23. When calling a function, passing a reference also forces the pointer to the stack.

24. The order in which constructor, destructor, value assignment constructor, and overloaded = are called: (all three functions have been implemented)

A) X x; X a = x;

 
 
  1. result:  
  2. X:construct  
  3. X:copy_struct   

B) X x; X a; a = x;

 
 
  1. Result:  
  2. X:construct  
  3. X:construct  
  4. X:copy_stru  
  5. operator =  
  6. X:destruct 

If no constructor is assigned, the result is:

 
 
  1. X:construct  
  2. X:construct  
  3. operator =  
  4. X:destruct   

(If X a = x directly, do not use a General constructor to call the copy constructor)

Pointer to the member function of the class: Set int X: a (void ){}

 
 
  1. X x;  
  2. int (X:: *pf)(void)= &X::a;  
  3. (x.*pf)();  

Pointer to the member variable: Set int I; to the member variable of X

 
 
  1. int X::*pm = &X::i;  
  2. X x; 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.