Function Pointer to Class Function

Started by
5 comments, last by tivolo 11 years, 2 months ago

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!

Advertisement

Member function pointers are fundamentally different from function pointers. Primarily because a member functions needs an object to be called. Search for member function pointers for a more in-depth explanation though, but the basic usage is as follows:

// define a member function pointer
void (info::*fptr)() = nullptr;
 
// assign the member function pointer to some member function
fptr = &info::think;
 
// call the member function on the player object
(player.*fptr)();

Ah, that makes complete sense. Thank you.

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.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

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.
http://tinyurl.com/shewonyay - Thanks so much for those who voted on my GF's Competition Cosplay Entry for Cosplayzine. She won! I owe you all beers :)

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.

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

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

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!

This topic is closed to new replies.

Advertisement