• Advertisement
Sign in to follow this  

Function Pointer to Class Function

This topic is 1801 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 tried searching for this for a bit but everything seemed a little advanced for me.  I have a function pointer:

 

void (*fun)();

 

Then I have class.

 

class info
{
public:
    void think();
};

extern class info player;

 

Is there a way to point fun to the think function?

 

I tried

 

fun=player.think;

 

but that got me

 

error C3867: 'info::think': function call missing argument list; use '&info::think' to create a pointer to member

 

I'm sure it's either something simple or that the two functions are just incompatible.  Could anyone please tell me what I'm missing?

 

Thanks for any help!

Edited by GameCreator

Share this post


Link to post
Share on other sites
Advertisement

And starting with C++11 we have std::function, which can largely abstract away this distinction (also available in Boost, for earlier C++).

 

It is generally preferable to use std::function in place of raw function pointers and member function pointers.

Share this post


Link to post
Share on other sites
I don't know if this helps but remember that you can also use static methods as function pointers in the typical C style.

So you could then pass the actual object as a parameter to be the "context".

i.e.
funcPtr(this);
Now within the static function (pointed to), you can still access private and protected data within the class.

Share this post


Link to post
Share on other sites

And starting with C++11 we have std::function, which can largely abstract away this distinction (also available in Boost, for earlier C++).
 
It is generally preferable to use std::function in place of raw function pointers and member function pointers.

And for non C++11 compilers there are multiple packages out there that mimic C# delegates, even when the function pointer is actually tied to an instance of a class. http://www.codeproject.com/search.aspx?q=native+C%2b%2b+delegates&doctypeid=1 Edited by NightCreature83

Share this post


Link to post
Share on other sites

I tried searching for this for a bit but everything seemed a little advanced for me.  I have a function pointer:

 

void (*fun)();

 

Then I have class.

 

class info
{
public:
    void think();
};

extern class info player;

 

Is there a way to point fun to the think function?

 

I tried

 

fun=player.think;

 

but that got me

 

 

error C3867: 'info::think': function call missing argument list; use '&info::think' to create a pointer to member

 

I'm sure it's either something simple or that the two functions are just incompatible.  Could anyone please tell me what I'm missing?

 

Thanks for any help!

 

If you are interested in the details behind pointers-to-member-functions, and how you can build an efficient delegate solution, I'd like to refer you to my blog post about exactly that: http://molecularmusings.wordpress.com/2011/09/19/generic-type-safe-delegates-and-events-in-c/

 

Hope that helps!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement