In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The purpose of this article is to share with you about how C++ implements function overloading. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Function overload
Function overloading also has an alias called function polymorphism. In fact, I personally feel that the name function polymorphism is easier to understand and more appropriate.
Functional polymorphism is a new feature of C++ based on C language, which allows us to use multiple functions of the same name. Of course, the parameters of these functions of the same name should be different. when we call a function, the compiler will automatically find the one we call from multiple functions of the same name according to the parameters we pass in. It is very close to the concept of polymorphism in object-oriented.
When we define a function, the compiler only looks at the number and type of parameters, regardless of the name of the parameter. As long as the number and type of parameters are not exactly the same, they are considered different functions.
For example:
Void print (const char * str, int width); void print (double d, int width); void print (long l, int width); void print (int I, int width); void print (const char * str)
The five functions listed above do not have exactly the same number and type of function arguments to each other, so they are considered different functions. When we use it, the compiler will use the corresponding function according to the parameters we pass in.
Print ('pancakes', 15); / / use 1print (' pancakes'); / / use 5print (1999.0, 10); / / use 2print (1999,23); / / use 4print (199L, 15); / / use 3
Of course there's no problem. What if we use it like this:
Unsigned year = 2021 leading print (year, 6)
We can find that the parameter type we pass in here is unsigned int, which does not match the input parameter type of any function. At this point, the compiler will not give up, but will try to force a match using standard type conversions. But here's the problem: we have three versions of a function whose first input parameter is a numeric type, so there are three ways to convert variables. At this time, C++ will reject this function call and report an error.
Similarly, some parameters that appear to be different from each other cannot coexist, such as:
Double cube (double x); double cube (double & x)
It seems that one is value passing and the other is reference passing, but it is obviously impossible for the compiler to tell which one we are going to call.
Another thing to note is the const modifier.
Void dribble (char * bits); / / 1void dribble (const char * bits); / / 2
There are two types of dribble functions, one for const pointers and one for regular pointers, and the compiler will decide which function to use based on whether the argument is const. Because it is legal to assign a non-exclusive value to a const variable, but vice versa.
In addition, the compiler distinguishes functions based on the number and type of parameters of the function, not on the return value of the function. So the following two statements are problematic:
Long gronk (int n, float m); double gronk (int n, float m)
Because they have the same number of parameters and the same type, although the return type is different, the compiler is still unable to distinguish.
This question often arises during interviews, and the interviewer will deliberately dig a hole and ask you what is the basis for function overloading. If the return types of the two functions are different, but the parameters are the same, can they be overloaded? Many students do not remember the concept of overloading very deeply, and they get hit when they are hot in the brain during the interview, so we must pay attention to it.
Thank you for reading! This is the end of the article on "how C++ realizes function overloading". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it out for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.