Sign in to follow this  

This topic is 4519 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 want to have a pointer to a function, so i have the following:
class MyClass()
{
   class MyMod()
   {
      typedef void(*PointFn)();

      PointFunc myfunction;
   };

   void Init(MyMod::PointFn afunction);
}

...

void MyClass::MyMod::Init(MyMod::PointFn afunction)
{
   // do stuff
}



// now this calls the Init function:
// assume i have a StandAloneBig() and a AnotherClass::Big() defined...

void StandAloneBig()
{
  // not in class
}


void AnotherClass::Big()
{
  // funcyion in class
}


// from within AnotherClass class, i call:


CMyClass.Init(&Big);  // doesnt work

// but:

CMyClass.Init(&StandAloneBig);  // does work


any ideas how i can get the pointer to the AnotherClass::Big function? error i get is:
error C2664: 'MyClass::MyMod::Init' : cannot convert parameter 2 from 'void(__thiscall AnotherClass::* )(void)' to 'MyClass::MyMod::Init'


Share this post


Link to post
Share on other sites
A function pointer must be generated by a static function, otherwise the compiler can't get the address...
so you have two ways:
- the direct approach declare the function static in Big
- or create a capsulation...


in the form
class BigCbProxy
{
static void function( void* clientData )
{
//now you must get to big...
static_cast<Big*>(clientData)->Big();
}
}

I hope this helps

Share this post


Link to post
Share on other sites
Quote:
Original post by Sparhawk42
A function pointer must be generated by a static function, otherwise the compiler


Not quite true. You can get and use pointers to member functions as well. However, it is true that the two are not interchangeable.

Share this post


Link to post
Share on other sites
Then it becomes:

typedef void (MyClass::*PointFunc)();

assuming the function is in MyClass. You have to include the name of the class the function is in (or using a templated function).

Then, to invoke the function, you need an instance (that's why you didn't make your function static, now didn't you?), and call:

PointFunc myfunction;
instance.*myfunction();

Share this post


Link to post
Share on other sites
Quote:
Original post by bilstonwarrior
yes i need it NOT to be static.... any ideas?
Use a function object:

struct A
{
...

struct FunctionObj
{
...

void operator () ()
{
...
}

...
};

...
FunctionObj * fn;
};

A a;
A::FunctionObj f;
a.fn = &f;
*(a.fn)(); // dispatch
a.fn->operator(); // dispatch


For cleaner dispatch syntax, eliminate the pointer indirection.

Share this post


Link to post
Share on other sites

This topic is 4519 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