我试图有一个适配器类,它有一个函数指针(比如fnPtr
)。并且根据不同的Adaptee类,fnPtr
分配相应的Adaptee的功能。以下是代码片段:
class AdapteeOne
{
public:
int Responce1()
{
cout<<"Respose from One."<<endl;
return 1;
}
};
class AdapteeTwo
{
public:
int Responce2()
{
cout<<"Respose from Two."<<endl;
return 2;
}
};
class Adapter
{
public:
int (AdapteeOne::*fnptrOne)();
int (AdapteeTwo::*fnptrTwo)();
Adapter(AdapteeOne* adone)
{
pAdOne = new AdapteeOne();
fnptrOne = &(pAdOne->Responce1);
}
Adapter(AdapteeTwo adtwo)
{
pAdTwo = new AdapteeTwo();
fnptrTwo = &(pAdTwo->Responce2);
}
void AdapterExecute()
{
fnptrOne();
}
private:
AdapteeOne* pAdOne;
AdapteeTwo* pAdTwo;
};
void main()
{
Adapter* adpter = new Adapter(new AdapteeOne());
adpter->AdapterExecute();
}
现在我面临的问题在于main()
功能。我没有任何方法可以调用适配器s function pointers (
fnptrOne and
fnptrTwo`)。我正进入(状态:
错误 C2276:“&”:对绑定成员函数表达式的非法操作
以及之前的错误消息。这可能意味着&
操作员无法从pAdOne->Responce1
.
这是否意味着我们可以t have a function pointer in some
ClassA which could point to a non-static function present in another
ClassB`?