Function overloading by changing parameters

About overriding a function in C ++ is legal, because both prototypes have a different number of parameters:

void func(int par1, double par2); void func(int par1, double par2, double par3); 

as is (because there is at least one parameter of another type):

 void func(int par1, double par2); void func(double par1, double par2); 

I wondered, just for curiosity, Is it possible to overload with the same number of parameters of the same type, but in a different order? For example, this is the following legal:

 void func(int par1, double par2); void func(double par1, int par2); 

If so, is there any official document?

+5
source share
3 answers

A formal document is an ISO C ++ standard, and yes, you can do it.

The entirety of ISO C ++ 11 Chapter 13 is about overloading, but the first few paragraphs summarize it:

If two or more different declarations are specified for the same name in the same area, this name is considered overloaded. By extension, two declarations in the same area that declare the same name but with different types are called overloaded declarations. Only function and function template declarations can be overloaded; variable and type declarations cannot be overloaded.

When an overloaded function name that refers to an overloaded function declaration is used in a call, it is determined by comparing the types of arguments at the point of use with the types of parameters in overloaded declarations that are visible at the point of use.

Overloading is possible if the parameter list is different, including the order. It differs in all three cases that you present in your question:

 {int, double} vs {int, double, double} {int, double} vs {double, double} {int, double} vs {double, int} 

Please note that overloading is not possible for two functions:

 void func(int par1, int par2); void func(int par2, int par1); 

since these are really only those types that provide uniqueness, not parameter names. Both of these functions are called func , and both of them have a list of parameters {int, int} , so they are not unique.

+9
source

Overloaded functions with the same number of parameters and the same types are allowed, but with a different order of corrections:

 void func(int a, double b) { } void func(double a, int b) { } 

When we list the exported characters, we got

 000000000000025d T _Z4funcdi 000000000000024f T _Z4funcid 

This means that the compiler exports two different functions to different addresses. The first is _Z4funcdi, d means double, and I means int.

+2
source

can you overload the same number of parameters of the same type, but in a different order?

If you think about it, there is no real reason that this is not legal. The compiler does not care about what the arguments of the function represent; it's not that smart. From this point of view, both definitions are different because they take different parameters.

The only thing you need to take care of in this particular case is to call the functions. If you, for example, have a class that has both operator double and operator int , the compiler will throw an error if you try to do:

 myClassWithBothCastOperators a, b; func(a,b); //<-- ERROR! 

Even so, you can still call the function by explicitly hovering objects:

 myClassWithBothCastOperators a, b; func((int)a, (double)b); //All fine here! 

However - I do not consider it a good design template, and the solution of one version of propper would be much clearer for people reading and using your code. As an example of real life, take a look at PHP implode . Although in C ++ you can see what types are variables, in PHP a variable can contain almost any type, and code analysis / debugging becomes a nightmare when implode / explode are used with variables with not very significant names.

+1
source

Source: https://habr.com/ru/post/1202565/


All Articles