I’m trying to retrieve a boolean value from one of two member functions. I would like to call a member function(main) that will point to one of two member functions based on a value passed to main…here is what I have so far:
class CRoutine{
...
BOOL (*MainRoutine(BOOL opcode))();
static BOOL SubRoutine1();
static BOOL SubRoutine2();
...
};
BOOL (*CRoutine::MainRoutine(BOOL opcode))()
{
switch ( opcode )
{
case false:
MessageBox(NULL, L"Routine1", L"Routine1", MB_OK);
return &CRoutine::SubRoutine1;
case true:
MessageBox(NULL, L"Routine2", L"Routine2", MB_OK);
return &CRoutine::SubRoutine2;
default:
MessageBox(NULL, L"Default Routine Selected", L"Routine1", MB_OK);
return &CRoutine::SubRoutine1;
}
}
BOOL CRoutine::SubRoutine1()
{
MessageBox(NULL, L"Routine1", L"Routine1", MB_OK);
return true;
}
BOOL CRoutine::SubRoutine2()
{
MessageBox(NULL, L"Routine2", L"Routine2", MB_OK);
return false;
}
winMain(...)
{
...
m_routine = new CRoutine();
BOOL result = m_routine->MainRoutine(0);
...
}
My question is:
How do I call MainRoutine() in order to get the boolean value from SubRoutine1() or SubRoutine2(). When I run the program as is I get the following error:
error C2440: ‘initializing’ : cannot convert from ‘BOOL (__cdecl *)(void)’ to ‘BOOL’
This is mostly theory I’m interested in, I know there are other ways of getting such a simple answer…thanks in advance.
Like this:
It is because you return function pointer from
MainRoutine(0), thus you need another pair of parentheses to invoke the function pointed by it.By the way, you are returning plain function pointer, not a member function pointer.