Mastering Function Pointers and Types in C++: A Practical Guide
This article explains the distinction between function pointers and function types in C++, shows how to declare, assign, and pass them as parameters, and demonstrates returning pointers to functions with clear code examples and typedef/decltype techniques.
Function Pointers and Function Types
Function pointers point to functions, not objects, and have a specific target type.
A function type is defined solely by its return type and parameter types, independent of the function name.
bool length_compare(const string&, const string&);The corresponding function type is: bool (const string&, const string&); And a pointer to such a function can be declared as:
bool (*pf)(const string&, const string&);Using Function Pointers
When a function name is used as a value, it automatically converts to a pointer.
pf = length_compare; // equivalent to pf = &length_compareFunction Pointer Parameters
A function type cannot be used directly as a parameter, but a parameter can be a pointer to a function.
When a function is passed as an argument, the compiler implicitly converts it to a function pointer.
typedef bool Func(const string&, const string&); // function type
typedef bool (*FuncP)(const string&, const string&); // function pointer type typedef decltype(length_compare) Func2; // function type
typedef decltype(length_compare) *Func2P; // function pointer typeNote: decltype(length_compare) yields the function type, not a pointer type.
using FTtype = int(int, int); // function type
typedef int (*pf)(int, int); // function pointer
int func(int a, int b) { return a + b; }
void print(int a, int b, FTtype fn) {
// The compiler implicitly converts <code>fn</code> to a function pointer
std::cout << fn(a, b) << std::endl;
}
int main() {
print(1, 2, func);
std::cout << typeid(FTtype).name() << std::endl;
std::cout << typeid(func).name() << std::endl;
std::cout << typeid(decltype(func)).name() << std::endl;
std::cout << typeid(pf).name() << std::endl;
return 0;
}The two declarations below are equivalent because the compiler automatically converts FTtype to a function pointer type:
void print(int a, int b, FTtype fn); void print(int a, int b, pf fn);Returning a Pointer to a Function
Although a function itself cannot be returned, a pointer to a function can be returned. Unlike parameters, the compiler does not automatically treat the return type as a pointer; it must be explicitly declared.
using F = int(int*, int);
using PF = int(*)(int*, int);
F f1(int); // error: F is a function type
PF f1(int); // correct: PF is a function pointer typeThe same function can be expressed with two alternative syntaxes:
int (*f1(int))(int*, int); auto f1(int) -> int(*)(int*, int);Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
