Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the function overloading function in Cpicket + language

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

I would like to share with you how to use the function overloading function in Chammer Craft + language. I believe most people don't know much about it, so share this article for your reference. I hope you will gain a lot after reading this article. Let's learn about it together.

"function overloading" means that there can be multiple functions with the same name, so the name is overloaded. These two terms refer to the same thing, but we usually use function overloading. You can design a series of functions through function overloading-- they do the same thing, but use different parameter lists.

An overloaded function is like a verb with multiple meanings. For example, Miss Piggy can cheer for her hometown team (root) on the baseball field, or she can grow (root) fungus crops in the fields. You can know what root means in each case according to the context. Similarly, C++ uses context to determine which version of the overloaded function to use.

The key to function overloading is the function parameter list, also known as function ftmction signature. If the number and type of parameters of two functions are the same, and the parameters are arranged in the same order, then their characters are the same, and the variable names are irrelevant. C++ allows you to define functions with the same name, provided they have different characters. If the number and / or type of parameters are different, the character is also different. For example, you can define a set of prototype print () functions as follows:

Void print (const char * str, int width); / / # 1Void print (double d, int width); / / # 2Void print (long 1, int width); / / # 3 Void print (int I, int width); / / # 4 frombyteVoid print (const char * str); / / # 5

When using the pnnt () function, the compiler uses prototypes with corresponding characters depending on the usage taken:

Print ("Pancakes", 15); / / use # 1print ("syrup"); / / use # 2print ("1999 4print 10"); / / use # 3print ("1999 LMagna 12"); / / use # 4print ("1999 LMagna 15"); / / use # 5 frombyte.com

For example, print ("Pancakes", 15) takes a string and an integer as parameters, which matches the # 1 prototype. When using an overloaded function, you need to use the correct parameter type in the function call For example, for the following statement:

Unsigned int year = 3210 (year, 6); / / ambiguous call

Which prototype does the Print () call match? It doesn't match the Ren A prototype! Circles that do not match will not automatically stop using one of these functions, because C++ will try to force a match using standard type conversions. If the # 2 prototype is the only prototype of print (), the function call print (year,6) converts the year to the double type. But in the above code, there are three prototypes that take a number as the first parameter, so there are three ways to convert year. In this case, C++ will reject the function call and treat it as an error.

Some features that look different from each other cannot coexist. For example, look at the following two prototypes:

Duble cube (double x); duble cube (double & x)

You might think that you can use function overloading here because their signatures look different. However, consider this from the compiler's point of view. Suppose you have code like this:

Cout.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report