Archived

This topic is now archived and is closed to further replies.

Making a Pointer to a Function

This topic is 5152 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m sorry if this has already been covered. My conn''s been acting too slow to use the search engine to look for it and I''m going crazy trying to fix it on my own these last couple days waiting for my conn to fix itself. I''m trying to make a pointer to a function and I take it that this; void (*PKillTasks) (int *, int *, int); PKillTasks = player.killTasks; is illegal because when I try to compile it, I get: error C2440: ''='' : cannot convert from ''void (__thiscall CPlayer::*)(int *,int *,int)'' to ''void (__cdecl *)(int *,int *,int)'' There is no context in which this conversion is possible What''s the right way to make a pointer to a function with pointers in it? And while we''re on the subject of pointers, is it ok to do this: //start function player.killTasks (within another "player" function) //pass two pointers and one variable as parameters killTasks (&*PLevel, &*PLevelXp, PTotalXp); It works, I just want to make sure there isn''t a "righter" way. __________________________________________ Don''t ask me, I''m the lazy programmer.

Share this post


Link to post
Share on other sites
Pointers to member functions are not compatible with pointers to non-member functions. Conversion is impossible.


void (CPlayer::*PKillTasks) (int *, int *, int);
PKillTasks = &CPlayer::killTasks;

(player.*PKillTasks)(arg1, arg2, arg3);


See function-pointer.org


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
It''s not the pointer arguments that are the problem but that the function you want a pointer to is a class member function.

When you want to declare a variable as a function pointer, the easiest approach is to declare the function signature as a type.

So, using the example that you''ve set forth

typedef void (*PKillTasks) (int *, int *, int);
PKillTasks pfnKillTasks = player.killTasks;

This syntax makes the declaration clearer - but isn''t required ( - and it still won''t work anyway). You could use

void (*PKillTasks) (int *, int *, int);

and then assign the function address to the pointer as you attempt to do. However, in your example the pointer and the function don''t have the same signature as the error message makes clear. The member function uses the "thiscall" calling convention and the pointer uses the "cdecl" calling convention. Under the "thiscall" convention, the "this" pointer is passed to the function - typically using the Ecx register rather than the stack. If the pointer was declared to use the "thiscall" convention, the signatures would match, but there would be no "this" there - or as the error message says - "no context in which the conversion is possible". If you declared the first argument of the pointer to be a pointer to CPlayer

void (*PKillTasks) (CPlayer *,int *, int *, int);

The signatures would be much closer than they were before, but then the expectation would be that the CPlayer * was passed via the stack rather than via Ecx. You could probably work around this using an assembly language thunk - but that much work probably isn''t warranted. And in the end, there''s still the matter of coming up with a "this" pointer. You probably should rethink what it is you''re trying to do and approach it from a different direction.

As far as "&*ptr" goes - the & and * cancel each other out. The asterik dereferences the pointer giving the value and the & takes the address of that value - which is back to the pointer. Just pass "ptr" and you should be good to go. Now, if you want to use & to indicate that the value should be taken as a reference, iirc there is a way to do that using slightly different syntax that I don''t remember off hand. I''m sure someone else will pop up with it soon. At any rate, it looks like you should rethink what it is you''re trying to do there and approach it from a direction that is more accomodating to passing arguments by reference.

Share this post


Link to post
Share on other sites
In addition to LessBread's comments on the &* notation...

If what you are trying to do is change the pointers to point to different objects, the correct notation is T*& pT. This makes pT a reference to a pointer of type T. This way all changes to the pointer effect the pointer that was passed in.

-Shags

-edit-
Uhm... never mind. I see what your trying do. Sorry.

[edited by - Shags on November 5, 2003 9:05:33 PM]

Share this post


Link to post
Share on other sites
It''s no problem not using that function pointer or the double function pointers; I was just trying to learn some more stuff.

Thanks.

__________________________________________
Don''t ask me, I''m the lazy programmer.

Share this post


Link to post
Share on other sites