C
Christian Nolte
I want to implement dynamic calling of interface methods using pointers to
members and have some trouble to get it right. An example of what I am
talking about looks like that:
///////////////////////////////////////////////
class StdInterface {
public:
int func(void) {return 1;}
int func2(void) {return 2;}
};
typedef int (StdInterface::*fp)();
class UseInterface {
public:
fp m_func;
};
///////////////////////////////////////////////
The next code excerpt shows how I want to use the pointer to meber:
///////////////////////////////////////////////
UseInterface *a = new UseInterface();
StdInterface *b = new StdInterface();
std::cout << b->func();
a->m_func = &StdInterface::func;
// here the compiler error C2065: 'm_func': not declared occurs (MS VC
..net):
std::cout << (a->*m_func)();
///////////////////////////////////////////////
Why does the compiler complain here? I have found an example of how to use a
pointer to member in Stroustrup (15.5 - Pointers to Members):
///////////////////////////////////////////////
//Example : Stroustrup (15.5 : Pointers to Members)
typedef int (StdInterface::*Pstd_mem)(); // pointer to member type
void f(StdInterface* p)
{
Pstd_mem s = &StdInterface::func;
//p->func(); // direct call
std::cout << (p->*s)() ; // call through pointer to member
}
////////////////////////////////////////////////
The above code works fine while mine is somehow wrong and I don't have a
clue on how to fix it. The only difference is, that my pointer is inside a
class, while the pointer in Stroustrups example is somehow
"function-scoped"...
Any help would be very much appreciated!
members and have some trouble to get it right. An example of what I am
talking about looks like that:
///////////////////////////////////////////////
class StdInterface {
public:
int func(void) {return 1;}
int func2(void) {return 2;}
};
typedef int (StdInterface::*fp)();
class UseInterface {
public:
fp m_func;
};
///////////////////////////////////////////////
The next code excerpt shows how I want to use the pointer to meber:
///////////////////////////////////////////////
UseInterface *a = new UseInterface();
StdInterface *b = new StdInterface();
std::cout << b->func();
a->m_func = &StdInterface::func;
// here the compiler error C2065: 'm_func': not declared occurs (MS VC
..net):
std::cout << (a->*m_func)();
///////////////////////////////////////////////
Why does the compiler complain here? I have found an example of how to use a
pointer to member in Stroustrup (15.5 - Pointers to Members):
///////////////////////////////////////////////
//Example : Stroustrup (15.5 : Pointers to Members)
typedef int (StdInterface::*Pstd_mem)(); // pointer to member type
void f(StdInterface* p)
{
Pstd_mem s = &StdInterface::func;
//p->func(); // direct call
std::cout << (p->*s)() ; // call through pointer to member
}
////////////////////////////////////////////////
The above code works fine while mine is somehow wrong and I don't have a
clue on how to fix it. The only difference is, that my pointer is inside a
class, while the pointer in Stroustrups example is somehow
"function-scoped"...
Any help would be very much appreciated!