function overloading in C + +

Source: Internet
Author: User

function overloading is used to describe a function that has the same or similar function, but a data type or a function management operation with different parameters.

Let's give an example of a practical application to illustrate the problem: we're going to do two different types of data and operate in order to achieve it, in C we have to write two different names of the number of Han to differentiate for example: int testa (int a,int b) and float testb (float A, FLOATB), so the word is no problem, but there is always a bad, so two have very similar operation function we have two different names, this is not very good management, so C + + to facilitate programmers to write programs specifically introduced the concept of function overload to solve this problem, we look at the following code:

#include <iostream>
using namespace Std;
int test (int a,int b);
Float test (float a,float b);
void Main ()
{
cout << Test (1,2) << Endl << Test (2.1f,3.14f) << Endl;
Cin.get ();
}
int test (int a,int b)
{
return a+b;
}
Float test (float a,float b)
{
return a+b;
}

In the above program we also used two functions named Test to describe int type and operation and float type and operation, which facilitates the programmer's management of the same or similar functional functions.

Read the above explanation many people will ask, so how can the computer judge the same name function? Does the operation cause a choice error?

The answer is in the negative. C + + Internal use of a kind of wit called name smashing to internally rename a function of the same name, the above example in the calculation of the renaming may be testii and TESTFF they are renamed by the type or number of parameters, about this as a programmer does not need to understand it, Just to explain the questions in your mind. Well, on the basics of functional overload here, as for how to use this function, we rely on the daily study or work is gradually groping.

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.