简体   繁体   中英

Function overloading and function pointers

The name of a function is a pointer to the function...
But in case of function overloading the names of two functions are the same...
So which function does the name point to?

It depends on the context; otherwise it's ambiguous. See this example (modified except below):

void foo(int a) { }
void foo(int a, char b) { }

int main()
{
    void (*functionPointer1)(int);
    void (*functionPointer2)(int, char);
    functionPointer1 = foo; // gets address of foo(int)
    functionPointer2 = foo; // gets address of foo(int, char)
}

You can do this in many ways, but the #1 rule?

Avoid casts!

Otherwise you'll break type safety and probably shoot yourself in the foot either then or later.
(Issues can come up with calling conventions, random changes you don't notice, etc.)

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