简体   繁体   中英

Overloading Function

If a function with the same name is defined in multiple files with a different signature, are these overloaded eg.

File1.cpp

int foo(){//do something}

File2.cpp

int foo(int a){//do something}

If I compile both these files in the same project, will these functions be treated as overloaded functions.

Yes they are two different functions.

A functions signature is primarily based on the arguments: The number of arguments, their types and the order. Class member functions also have modifiers, like const or not. Return type is not part of the signature to distinguish between overloaded functions.

If two functions of the same name have unique signatures, then they are different.

Yes, they will be appropiately name-mangled into two separate functions whose scope is global, at the global namespace (aka: the :: namespace).

For two functions to be overloaded, and not violate the ODR , their parameter types and "attributes" (such as const , volatile , or noexcept after the argument list) shall be different. Two functions with the same set of parameter types and "attributes", but with different return type violate the ODR.

Just don't worry about this, and let the linker be the slave of the compiler it was born to be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM