You really should read the compiler's diagnostics even with subsequent attempts of solving a compile error. I really doubt that it ever said "nope."
A typedef
is especially useful when you want to declare a function (or a function pointer) returning a function pointer. In a simple function pointer type expression it doesn't buy you much, as you really only need to remember the parenthesis when you write one, like this:
int *ShowPopupMessage(const char*, char); // a function declaration
int (*ShowPopupMessage)(const char*, char); // a function pointer definition
Both are an easy read for C++ programmers.
In your case, a typedef
might be preferable (as you need to use the type twice), but as you
seem to have trouble understanding acceptable implicit conversions, I'm not going to hide the problem behind a typedef
curtain.
In the examples above, you're mostly only changing the left-hand side of things. C++ does not allow implicit conversions of integer types to (any) pointer types, and in a similar fashion, it doesn't allow implicit conversions of object pointer types to function pointer types. If you want to interpret an integer as a function pointer, you need a cast -- and not just any cast, but a reinterpret_cast
to a function pointer type.
// this is OK (with the cast), but ShowPopupMessage is not a function pointer,
// but a pointer to int
int *ShowPopupMessage = (int*)0xDEADBEEF;
// this is incorrect, as C++ does not allow implicit conversions from
// object pointers to function pointers
int (*ShowPopupMessage)(const char*, char) = (int*)0xDEADBEEF;
// this is OK (assuming you know what you're doing with 0xDEADBEEF)
int (*ShowPopupMessage)(const char*, char) =
(int(*)(const char*, char))0xDEADBEEF;
// this is preferable in C++ (but it's not valid C)
int (*ShowPopupMessage)(const char*, char) =
reinterpret_cast<int(*)(const char*, char)>(0xDEADBEEF);
// (this is a possibility in C++11)
#include <functional>
std::function<int(const char*, char)> ShowPopupMessage =
reinterpret_cast<int(*)(const char*, char)>(0xDEADBEEF);
// it's used in much the same way as function pointers are
// i.e. you call it like you always call them: ShowPopupMessage("", ' ')