Before that I used such huge things:
int* ShowPopUpMessage = (int*)0x004837F0; //declare
((int (__cdecl*)(const char *, char))ShowPopUpMessage)("You see this message!", 0); //call
ehm, I think is no need to tell it’s kinda confusing.
Now I want to declare normal function pointer like that:
int *ShowPopupMessage(const char *, char);
To be able call that function like that:
ShowPopupMessage("asd", 0);
But I can’t assign function address to that pointer. I tried:
int *ShowPopupMessage(const char *, char) = (int*)0x004837F0; //error: function "ShowPopupMessage" may not be initialized
//then this
int *ShowPopupMessage(const char *, char);
ShowPopupMessage = 0x004837F0; //nope
ShowPopupMessage = (int*)0x004837F0; //nope
*ShowPopupMessage = 0x004837F0; //nope
*ShowPopupMessage = (int*)0x004837F0; //nope
uhh. Any other ways?
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
typedefis 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:Both are an easy read for C++ programmers.
In your case, a
typedefmight be preferable (as you need to use the type twice), but as youseem to have trouble understanding acceptable implicit conversions, I’m not going to hide the problem behind a
typedefcurtain.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_castto a function pointer type.