I was trying to have an Adapter class, which has a Function Pointer (say fnPtr
). And based on different Adaptee classes the fnPtr
will be assigned with corresponding adaptee`s function.
Following is the code snippet:
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();
}
Now the problem I am facing is in the main()
function. I am not getting any way to call Adapters function pointers (
fnptrOneand
fnptrTwo`).
I am getting:
error C2276: '&' : illegal operation on bound member function expression
along with the previous error message. This could mean that &
operator is not able to create a function pointer out of pAdOne->Responce1
.
Does it mean that we cant have a function pointer in some
ClassAwhich could point to a non-static function present in another
ClassB`?