function overloading and templates in C + +

Source: Internet
Author: User
Tags types of functions


Functions are like tools, there are many kinds of tools, so let's take a screwdriver as an example to feel some similarity to the function overloading and the template.

There are many kinds of screwdriver, common is a font and plum type, each type from small to large and have a lot of numbers. We are based on the screw style and size

Choose a screwdriver of that type and select a large size. The screwdriver did one thing, screwing the screws, that is to say, a variety of screwdrivers their function is phase

The same, just different types.

First, function overloading

In C + +, it is allowed to use a function name to express the same function, except that the operation type (parameter number and type) are different functions, which is the function overloading.

The compiler is able to make optimal allocations based on their respective arguments and parameters and the number of arguments, which automatically determines which function body to invoke. Look at the following example

Child

<span style= "FONT-SIZE:18PX;" > #include <iostream>using namespace std;int <span style= "color: #ff0000;" >add</span> (int x,int y)         //Word <span style= "color: #ff0000;" > Screw Knife </span> (Word, 5th) {cout<< "(int,int) \ t"; return x+y;} Double <span style= "color: #ff0000;" >add</span> (double x, double y)//Plum <span style= "color: #ff0000;" > Screw Knife </span> (Plum, No. 5th) {cout<< "(double,double) \ t"; return x+y;} </span>

Understand the function overloading we also have to pay attention to a problem:

If the function name is the same, the formal parameter list is the same, only the return type is different, is illegal. As in the example below, when a word 5th screwdriver is required

, I do not know which type of screw cutter to choose.

<span style= "FONT-SIZE:18PX;" ><span style= "color: #ff0000;" >int</span> Add (int x,int y)         //<span style= "color: #ff0000;" > Word </span> spiral Knife (Word, No. 5th) {cout<< "(int,int) \ t"; return x+y;} <span style= "color: #ff0000;" >double</span> Add (int x,int y)//<span style= "color: #ff0000;" > Plum </span> Spiral Knife (Word, No. 5th) {cout<< "(double,double) \ t"; return x+y;} </span>


Second, function template

Screwdriver machines can produce different types of screwdrivers, where there is a function template that can generate different types of functions.

The form of the template is:

<span style= "FONT-SIZE:18PX;" >template <class type name 1,class type name 2,...> return type   function name (formal parameter list) {function body;} </span>


Let's take a look at how the screwdriver was made.

<span style= "FONT-SIZE:18PX;" > #include <iostream>using namespace Std;template < style class, Specification class > Style 1 screw cutter (style 1, size 1) {processing; return finished;} int main () {cout << Screw Knife (Word, no. 5th) <<endl;cout<< screw Cutter (plum, 5th) <<endl;return 0; }</span>


The final finished products are one-word spiral knife (one word, No. 5th) and plum blossom screw (plum, 5th)

Iii. Differences and linkages

Multiple overloaded functions with the same logical functionality as function parameters and function value types are described by a function template that creates a common function

function, support many different formal parameters, simplify the function body design of overloaded function, make the reusability of code greatly improve, thus improve the software development efficiency

Rate. What tools do we need, the parameters to the machine, directly processed out to use.

function overloading and templates in C + +

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.