Pointers to Overloaded Functions

As usual, when we use an overloaded function, the context must make it clear which version is being used. When we declare a pointer to an overloaded function

void ff(int*);void ff(unsigned int);void (*pf1)(unsigned int) = ff;  // pf1 points to ff(unsigned)

the compiler uses the type of the pointer to determine which overloaded function to use. The type of the pointer must match one of the overloaded functions exactly:

void (*pf2)(int) = ff;    // error: no ff with a matching parameter listdouble (*pf3)(int*) = ff; // error: return type of ff and pf3 don't match

Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.