Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function overloading and function pointers

Tags:

c++

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?

like image 547
AvinashK Avatar asked May 31 '11 04:05

AvinashK


People also ask

What is function pointer?

A pointer to a function points to the address of the executable code of the function. You can use pointers to call functions and to pass functions as arguments to other functions.

What is difference between function and pointer?

Originally Answered: What is the difference between pointer to function and function pointer? A function pointer points to the memory address, where the function's code is stored. So unlike other functions function pointer points to code rather than data. e.g.

What is the difference between function and function overloading?

Functions have same name but different number or different type of parameters. Functions have same name ,same number and same type of parameters. Overloading of the functions take place at compile time. Overriding of the functions take place at run time.

What is function overloading?

An overloaded function is really just a set of different functions that happen to have the same name. The determination of which function to use for a particular call is resolved at compile time. In Java, function overloading is also known as compile-time polymorphism and static polymorphism.


1 Answers

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.)

like image 90
user541686 Avatar answered Oct 31 '22 03:10

user541686