Jump to content
  • Advertisement
Sign in to follow this  

Pointers to class member functions

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

Hi! I use MSVC++ 6.0. I am wondering how can I get a pointer to a function that is a class member. Does the member function needs to be defined as 'static' ?? Thanks! :)

Share this post

Link to post
Share on other sites
Thanks, TomasH. Unfortunately, I didn't find my answer there.

I'll try to be more precise. Take a look at the following code :

// *** somwhere in GameEngine class *** //

// Pointer to the main program routine
int (*fMainRoutine) (float dt);

// Sets a game main routine pointer
void SetMainRoutine (int (*f) (float) = NULL) { fMainRoutine = f; };

// *** inside IntroModule class *** //

int HandleMain(float dt) { return 1; }

// *** somewhere in a code *** //

GameEngine.SetMainRoutine (IntroModule::Instance().HandleMain);

The error I get is :
error C2664: 'SetMainRoutine' : cannot convert parameter 1 from 'int (float)' to 'int (__cdecl *)(float)'
None of the functions with this name in scope match the target type

Forgive me my stupid questions but I don't know how to solve the problem myself. What can I do ??

Thanks :)


Share this post

Link to post
Share on other sites

class foo {
void bar() { printf ( "foo::bar\n" ); }
void baz() { printf ( "foo::baz\n" ); }

int main() {
// declare memFuncPointer to of type 'pointer to void function
// member of foo class'.

foo f;

// assign memFuncPointer to the bar member function
memFuncPointer = foo::bar;

// call memFuncPointer (bar) on f

// assign memFuncPointer to the baz member function
memFuncPointer = foo::baz;

// call memFuncPointer (baz) on f

return 0;

As you can see, the syntax of pointers to member-functions is significantly uglier than pointers to free functions.

The alternative is, as you suggested, to make the member function static, in which case you can use the same syntax as would for free functions.

- Neophyte

P.S. The link provided by TomasH contained all the information you were looking for if you had bothered to actually read the page.

Share this post

Link to post
Share on other sites
Hi you cannot assign a member function to a normal pointer to function and you can't assign it that way besides, this is how it works like so:

#include <iostream>

struct a {
void do_func() const { std::cout << "doing it\n"; }

typedef void (a::*PMF)(void) const;

int main() {
a i;

PMF f = &a::do_func;

//direct call

//indirect call
return 0;

You can't use a pointer to member function with-out some instance of the type.

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!