-
September 4th, 2002, 09:34 AM
#1
Get the address of a member function
Hi
I have a static structure with a member int (*repfn)(int state, const char * reply,int tout) – a function pointer.
I want to initialise the pointer within my dialog constructor to point to a member function of my dialog class.
Like this:
Code:
CEnterDiagnostics::CEnterDiagnostics() : CPropertyPage(CEnterDiagnostics::IDD)
{
//{{AFX_DATA_INIT(CEnterDiagnostics)
//}}AFX_DATA_INIT
StateMachine[0].repfn = OpenReply;
}
Where OpenReply is a member of my class CEnterDiagnostics and declared as
int OpenReply(int state, const char *pReply,int tout);
But it doesn’t work I get a compiler error
C2440: '=' : cannot convert from 'int (__thiscall CEnterDiagnostics::*)(int,const char *,int)' to 'int (__cdecl *)(int,const char *,int)'
Is there a way to do this?
Thanks
Alec
-
September 4th, 2002, 09:43 AM
#2
If it's static, try &OpenReply.
Jeff
-
September 4th, 2002, 09:53 AM
#3
OpenReply was not static, I tried making it static and it compiles OK.
Can you only get a pointer to a static member function?
Thanks for the help.
-
September 4th, 2002, 09:59 AM
#4
For function pointers, I use boost::function which is freely available. You can find it at http://www.boost.org/, along with a lot of other template classes.
Code:
// define functor
boost::function<int, CEnterDiagnostics*, int, const char*, int> repfn;
...
// assign functor
repfn = &OpenReply;
....
// call functor
repfn(enterDiagnosticsPtr, state, reply, tout);
Used in conjunction with boost::bind, this is very powerful.
Jeff
-
September 4th, 2002, 10:13 AM
#5
Originally posted by AlecJames
OpenReply was not static, I tried making it static and it compiles OK.
Can you only get a pointer to a static member function?
Thanks for the help.
It's obvious that it won't work like you tried it with a non-static member function. These always recieve as a minimum the pointer to the instance of the object as a parameter, so you'd have to add that to your function declaration somehow.
But then again, this might not make sense. How does the code which calls repfn know which instance of CEnterDiagnostics to refer to ?
I'm really wondering how boost achieves this...
[edit : ups, I just realized that the answer to this question was already provided by Jeff
repfn(enterDiagnosticsPtr, state, reply, tout);
So the first parameter is the pointer to the instance of CEnterDiagnostics ]
Last edited by Yves M; September 4th, 2002 at 10:15 AM.
-
September 4th, 2002, 12:20 PM
#6
noboost - just C++ (in case you#re looking for...)
To declare a pointer to a member function:
struct cfoo {
int bar(int x);
};
int (cfoo::*pFooBar)(int) = cfoo::bar;
// to call, you need an actual instance -
foo.*pFooBar(17);
pFoo->*pFooBar(42);
of course, functors are much more expressive and powerful... ;-)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|