Introduction to function overloading in C ++

Source: Internet
Author: User

For C ++ as a language to be compatible with C, C ++ retains some procedural languages and features, names such as foo_int_int include the function name, number of function parameters, and type information. C ++ relies on this mechanism to implement function overloading.

Therefore, it can define global variables and functions that do not belong to any class. However, C ++ is an object-oriented programming language after all. To support function overloading, C ++ treats global functions differently from C. Starting from the standard header file, an enterprise once gave the following interview questions: why are there similar structures in the standard header files?

 
 
  1. #ifndef __INCvxWorksh  
  2. #define __INCvxWorksh   
  3. #ifdef __cplusplus  
  4. extern "C" {  
  5. #endif   
  6. /*...*/   
  7. #ifdef __cplusplus  
  8. }  
  9. #endif   
  10. #endif /* __INCvxWorksh */ 

Obviously, the compilation macro "# ifndef _ INCvxWorksh, # define _ INCvxWorksh, and # endif" in the header file is used to prevent the header file from being repeatedly referenced. So what is the role? We will discuss it one by one below.

3. the deep encryption extern "C" extern "C" contains double meanings, which can be obtained literally: first, the target to be modified is "extern". Second, the target to be modified is "C. Let's explain these two meanings in detail. The function or variable specified by extern "C" is of the extern type;

 
 
  1. #ifdef __cplusplus  
  2. extern "C" {  
  3.  #endif   
  4.  #ifdef __cplusplus  
  5. }  
  6. #endif  

Extern is the keyword that indicates the range visibility of functions and global variables in C/C ++. This keyword tells the compiler, the declared functions and variables can be used in this module or other modules. Remember, the following statement: extern int a; is just a declaration of a variable. It is not defining variable a and does not allocate memory space for. Variable a can only be defined once as a global variable in all modules. Otherwise, a connection error occurs.

  • Describes the problems and skills of the C ++ Language
  • Exploring various concise and flexible features of C ++
  • How to Use the Visual C ++ subset to search for a topic
  • How to better learn C ++ -- and a summary of C ++ learning methods
  • Advanced Programming instructions on C ++ learning methods

In general, the function and global variables referenced by this module to other modules are declared with the keyword extern in the module header file. For example, if Module B wants to reference the global variables and functions defined in module A, it only needs to include the header file of module. In this way, when Module B calls A function in module A, although Module B cannot find the function in the compilation phase, no error is reported; it will find this function from the target Code Compiled by module A in the connection phase.

The keyword corresponding to extern is static. The global variables and functions modified by extern can only be used in this module. Therefore, a function or variable can only be used by this module and cannot be modified by extern "C. As an object-oriented language, modified by extern "C" supports function overloading, while Procedural Language C does not. The name of the function in the symbol library after being compiled by C ++ is different from that in the C language. For example, assume that the prototype of a function is:

 
 
  1. // Module A header file moduleA. h
  2. # Ifndef MODULE_A_H
  3. # Define MODULE_A_H
  4. Int foo (int x, int y );
  5. # Endif

After the function is compiled by the C compiler, its name in the symbol library is _ foo, while the C ++ compiler may generate different compilers with different names, such as _ foo_int_int, but all adopt the same mechanism, and the new name is called "mangled name ").

A name such as foo_int_int contains the function name, number of function parameters, and type information. C ++ relies on this mechanism to implement function overloading. For example, in C ++, the void foo (int x, int y) and void foo (int x, float y) functions generate different symbols, the latter is _ foo_int_float.

Similarly, variables in C ++ support both class member variables and global variables in addition to local variables. The class member variables of the program written by the user may have the same name as the global variables, which are distinguished. In essence, the compiler uses a unique name for the variables in the class when compiling, similar to the function processing. This name is different from the global variable name with the same name in the user program.

Related Article

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.