Sign in to follow this  

Pointers to class member functions

This topic is 4870 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 {
public:
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'.
void(foo::*memFuncPointer)(void);

foo f;

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

// call memFuncPointer (bar) on f
(f.*memFuncPointer)();

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

// call memFuncPointer (baz) on f
(f.*memFuncPointer)();

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
i.do_func();

//indirect call
(i.*f)();
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

This topic is 4870 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this