0

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 (fnptrOneandfnptrTwo`). 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 someClassAwhich could point to a non-static function present in anotherClassB`?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Anitesh
  • 27
  • 6
  • 3
    Just as a hint, you might want to read about [`std::function`](http://en.cppreference.com/w/cpp/utility/functional/function) and [`std::bind`](http://en.cppreference.com/w/cpp/utility/functional/bind). I explain how to use them in [this old answer of mine](http://stackoverflow.com/questions/14189440/c-class-member-callback-simple-examples/14189561#14189561). – Some programmer dude Oct 21 '13 at 10:51
  • Also you may want to read [this](http://www.parashift.com/c++-faq/pointers-to-members.html). – BartoszKP Oct 21 '13 at 10:53

1 Answers1

1

When assigning a member function pointer, you assign it the class member function pointer, as in AdapteeTwo::Responce2.

So it should be e.g.

fnptrTwo =  &AdapteeTwo::Responce2;

You use the object when calling the member function pointer:

(pAdTwo->*fnptrTwo)()

This last statement calls the function pointed to by fnptrTwo in the pAdTwo object, so pAdTwo will be this inside the called member function.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I tried the same as follow: fnptrOne = &(AdapteeOne::Responce1); fnptrTwo = &(AdapteeTwo::Responce2); But got error saying: "error C2064: term does not evaluate to a function taking 0 arguments. The error statement is very confusing. Please comment.". I can see the error is coming because both the functions are member functions, so implicit parameter "this" is passed as a parameter. – Anitesh Oct 21 '13 at 11:02
  • @Anitesh I'm not saying anything about how you declare the function pointers, it's how you assign and call them that you have to change. – Some programmer dude Oct 21 '13 at 11:04
  • Please have a look on my first attempt code: `class Adapter { public: int (AdapteeOne::*fnptrOne)(void); int (AdapteeTwo::*fnptrTwo)(void); Adapter(AdapteeOne* adone) { fnptrOne = &(AdapteeOne::Responce1); this->*fnptrOne(); } Adapter(AdapteeTwo adtwo) { fnptrTwo = &(AdapteeTwo::Responce2); } }; void main() { Adapter* adpter = new Adapter(new AdapteeOne()); (pAdTwo->*fnptrTwo)(); }` The above code was not working and giving error C2064, mentioned above. Had i misunderstood what you said? – Anitesh Oct 21 '13 at 11:18
  • @Anitesh But you haven't declared any variables `pAdTwo` or `fnptrTwo` in the `main` function, so yes that will give you compilation errors. Also in the constructor for `AdapteeOne` you forgot the parentheses around the object (and it's not `this` you should be calling the function on but `adone`) and function pointer, which will also cause an error. – Some programmer dude Oct 21 '13 at 11:19
  • @Anitesh I really recommend you to read my comment about `std::function` and `std::bind`. It will make your life much easier in the end if you start using them. – Some programmer dude Oct 21 '13 at 11:20
  • Thanks a lot for your help and for the information about the above mentioned two functions. It is working fine now. – Anitesh Oct 22 '13 at 09:04