Jump to content

  • Log In with Google      Sign In   
  • Create Account


Function Pointer to Class Function


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 GameCreator   Members   -  Reputation: 656

Like
0Likes
Like

Posted 12 February 2013 - 10:43 AM

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, 12 February 2013 - 10:43 AM.


Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 7745

Like
7Likes
Like

Posted 12 February 2013 - 11:05 AM

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)();


#3 GameCreator   Members   -  Reputation: 656

Like
0Likes
Like

Posted 12 February 2013 - 11:17 AM

Ah, that makes complete sense.  Thank you.



#4 swiftcoder   Senior Moderators   -  Reputation: 9507

Like
3Likes
Like

Posted 12 February 2013 - 11:21 AM

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 - Software Engineer @Amazon - [swiftcoding]


#5 Karsten_   Members   -  Reputation: 1366

Like
0Likes
Like

Posted 12 February 2013 - 01:10 PM

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.

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


#6 NightCreature83   Crossbones+   -  Reputation: 2652

Like
0Likes
Like

Posted 12 February 2013 - 02:17 PM

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, 12 February 2013 - 02:19 PM.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#7 tivolo   Members   -  Reputation: 869

Like
3Likes
Like

Posted 12 February 2013 - 03:51 PM

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!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS