Knowledge about function overloading in c ++

Source: Internet
Author: User

Function Overloading is used to describe functions of the same name that have the same or similar functions, but the data type or function management operations with different parameters!

Let's take an example in practical application to illustrate the problem: we need to perform two different data types and operations in order to implement it, in C language, we need to write two Han numbers with different names to distinguish them: int testa (int a, int B) and float testb (float a, floatb ), there is no problem with this word, but it is always a little bad. We have two different names for these two very similar operation functions, which is not very well managed, therefore, c ++ introduces the concept of function overloading to help programmers write programs. Let's take a look at the following code!

// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# 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, this makes it easier for programmers to manage functions with the same or similar functions!

After reading the above explanation, many people will ask, how can a computer determine the Same Name function? Will the selection be incorrect during the operation?

The answer is no. c ++ internally uses a wit called name crushing to rename a function of the same name internally, the above example may be testii and testff after the computation and renaming. They rename the parameter internally through the parameter type or number. You do not need to know about this as a programmer, let's just explain your questions. Well, let's talk about the basics of function overloading. As for how to use this function, it is up to you to gradually explore in your daily study or work!

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.