Function overloading in C ++ Heavy Load Technology

Source: Internet
Author: User

In C language, if we need to print integer, floating point, and string data, we need to write functions with different function names to complete the corresponding functions, as shown below:

Void print_int (INT );

Void print_float (float );

Void print_string (char STR []);

These three functions are used to output integer, floating point, and string data respectively. In C language, function IDs are identified by function names.

However, in C ++, although these three functions output different data, their functions are the same, therefore, it is not satisfied that the above method is used to compile functions for various types of data separately. To solve this problem, C ++ provides the heavy load technology. The implementation of various constructors is a typical application of the heavy load technology.

In C ++, how does one implement overload for functions with the same name? How does the C ++ compiler identify and call correct functions?

In the above example, the print program can be as follows:

Void print (INT );

Void print (float );

Void print (string );

After seeing these three functions with the same function name, the compiler will change the names of the three functions:

_ Print_int;

_ Print_float;

_ Print_string;

Then the compiler can uniquely identify them.

Every function is composed of the function name, function parameter, and function return value. c ++ can use the function name and function parameter to uniquely identify the function, so why cannot I use the function return value? I think for two functions with the same function name and parameter, if the return values are different, C ++ can also perform forced type conversion when the function returns, therefore, the compiler cannot identify which function is called. Of course, in addition to the methods described above, there are also methods to identify functions through function scopes.

As follows:

Class

{

Public:

A ()

{Cout <"A () is in Class A" <Endl ;}

}

 

Void ()

{Cout <"A () in main ()" <Endl ;}

 

The above two functions with the same name and parameters, one is a class member function and the other is a global function. Although the names and parameters are the same, they are defined in different positions, therefore, they are still two different functions. When using class member functions, the scope operator ":" is required.

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.