Function Pointers

Started by
16 comments, last by Zakwayda 14 years, 12 months ago
have you looked at boost functions? why reinvent the wheel?
Advertisement
Try this and see if it does you any good: :)

Character.h
#define CALL_MEMBER_FUNC(object,ptrToMember)  ((object).*(ptrToMember))()class Character{	public:	    Character(void);	    ~Character(void);			    void Sleep();	 // All the other funcitons	 private:			            void (Character::*pDecisionFunciton)();};


Character.cpp
pDecisionFunciton = &Character::Sleep;	CALL_MEMBER_FUNC(*this, pDecisionFunciton);
Quote:Original post by Shadow Wolf
Try this and see if it does you any good: :)

Character.h
*** Source Snippet Removed ***

Character.cpp
*** Source Snippet Removed ***
No need for macros:

template < typename T >void CallMemberFunction(T& object, void (T::*function)()){    ((object).*(function))();}// ...pDecisionFunction = &Character::Sleep;	CallMemberFunction(*this, pDecisionFunction);
Quote:Original post by Shadow Wolf
Try this and see if it does you any good: :)

Character.h
*** Source Snippet Removed ***

Character.cpp
*** Source Snippet Removed ***


It compiles fine now, but It doesnt actually call the function its pointing to. So i guess this is a problem with the macro? Can anyone help here?
Quote:It compiles fine now, but It doesnt actually call the function its pointing to. So i guess this is a problem with the macro? Can anyone help here?
It should work (although, again, I wouldn't recommend using a macro).

How do you know the function isn't being called? Can you post your code?
Sorry it was my mistake.

I'd missed the brackets at the end of the macro.

Thanks for all your help.
#define CALL_MEMBER_FUNC(object,ptrToMember)  ((object).*(ptrToMember)) 


template < typename T >void CallMemberFunction(T& object, void (T::*function)()){    ((object).*(function))();}


I'm not entirely convinced either of those has any purpose, seeing that they both make you type more to achieve something relatively simple, and the syntax for calling member function through the pointer is unmistakeable for anything else because of the special .* operator.

If it is for inexperience programmers that may not be familiar with the syntax, one could show that to them, and if they don't get it they should be prohibited from using pointers to methods.

Besides, wouldn't you need a whole set of overloads in case the method takes any parameters?

---------

Or perhaps these are for providing a simple function-call like syntax (for who knows which templated use). But then shouldn't these be a struct with the () operator and doesn't std::mem_fun_ref already do this (and boost::mem_fn even better)?
Quote:Original post by visitor
*** Source Snippet Removed ***

*** Source Snippet Removed ***

I'm not entirely convinced either of those has any purpose, seeing that they both make you type more to achieve something relatively simple, and the syntax for calling member function through the pointer is unmistakeable for anything else because of the special .* operator.

If it is for inexperience programmers that may not be familiar with the syntax, one could show that to them, and if they don't get it they should be prohibited from using pointers to methods.

Besides, wouldn't you need a whole set of overloads in case the method takes any parameters?

---------

Or perhaps these are for providing a simple function-call like syntax (for who knows which templated use). But then shouldn't these be a struct with the () operator and doesn't std::mem_fun_ref already do this (and boost::mem_fn even better)?
My intent was mainly to demonstrate that there's no reason to use macros for this. To be sure, the shortcut is of limited usefulness, and is made largely irrelevant by the tools that you mentioned (boost::function, boost::bind, etc.).

I don't know that such a convenience function would be *completely* pointless in all situations, but honestly I don't feel strongly enough about it to try and make a case for it :) Really, the goal was just to demonstrate to the OP how to do it without using macros if, for whatever reason, the OP does want to use a shortcut of this sort.

This topic is closed to new replies.

Advertisement