17

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?

AvinashK
  • 3,309
  • 8
  • 43
  • 94
  • 2
    you may want to look at a similar question: http://stackoverflow.com/questions/2942426/how-to-specify-a-pointer-to-an-overloaded-function – davka May 31 '11 at 04:13

1 Answers1

21

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

Community
  • 1
  • 1
user541686
  • 205,094
  • 128
  • 528
  • 886
  • +1. @Mehrdad and @Ben: Point Noted which you two said in my post. Thanks :-) – Nawaz May 31 '11 at 04:38
  • @Nawaz: Come to think of it, maybe I should mention that in my post too. I'll add it. :) – user541686 May 31 '11 at 04:50
  • Yeah. That would be better, more complete. :-) – Nawaz May 31 '11 at 04:51
  • 1
    Sometimes casts are necessary. I have run into this when trying use boost::bind with overloaded member functions. Whenever possible, it is best to rename the functions to avoid the cast. – IronMensan May 31 '11 at 13:56