Function overloading in C ++

Possible duplicate:
List of name resolution rules (and overloading) in C ++

What are the rules in C ++ for how the compiler decides which function to choose? (which is given two functions with the same name - how the compiler selects / pauses one function over another, basically I want to know what types of compiler casting are more willing to do when it selects)

+6
source share
3 answers

As already mentioned, the rules are fully described in the standard. As a basic rule, the compiler will choose an overload that requires the least automatic transformations, with the caveat that it will never apply 2 custom transforms.

Integer types are automatically thrown around. Therefore, if you have a function overloaded with int and double , the compiler will select the int function if called with a constant, which is an integer. If you did not have an int version, the compiler will select double . And among the various integer types, the compiler prefers int for integer constants, because that is their type. If you are overloaded with short and unsigned short , but called with constant 5 , the compiler will complain that it cannot determine which overload to use.

Scott Meyers' book does have the best explanation I've ever read.

+4
source

The full name of the function consists of what you called the function, as well as a list of parameters. So, logically, 2 functions are called the same, but with different parameter lists have different "full names". My terminology is probably a bit off, so if anyone wants to fix it, feel free to.

0
source

It is based on the type of argument (s). No involvement, if the type does not match it, will simply not compile.

0
source

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


All Articles