Note that there are some explanatory texts on larger screens.

plurals
  1. POCan we create an Adapter patterns using function pointers?
    primarykey
    data
    text
    <p>I was trying to have an Adapter class, which has a Function Pointer (say <code>fnPtr</code>). And based on different Adaptee classes the <code>fnPtr</code> will be assigned with corresponding adaptee`s function. Following is the code snippet:</p> <pre><code>class AdapteeOne { public: int Responce1() { cout&lt;&lt;"Respose from One."&lt;&lt;endl; return 1; } }; class AdapteeTwo { public: int Responce2() { cout&lt;&lt;"Respose from Two."&lt;&lt;endl; return 2; } }; class Adapter { public: int (AdapteeOne::*fnptrOne)(); int (AdapteeTwo::*fnptrTwo)(); Adapter(AdapteeOne* adone) { pAdOne = new AdapteeOne(); fnptrOne = &amp;(pAdOne-&gt;Responce1); } Adapter(AdapteeTwo adtwo) { pAdTwo = new AdapteeTwo(); fnptrTwo = &amp;(pAdTwo-&gt;Responce2); } void AdapterExecute() { fnptrOne(); } private: AdapteeOne* pAdOne; AdapteeTwo* pAdTwo; }; void main() { Adapter* adpter = new Adapter(new AdapteeOne()); adpter-&gt;AdapterExecute(); } </code></pre> <p>Now the problem I am facing is in the <code>main()</code> function. I am not getting any way to call Adapter<code>s function pointers (</code>fnptrOne<code>and</code>fnptrTwo`). I am getting:</p> <blockquote> <p>error C2276: '&amp;' : illegal operation on bound member function expression</p> </blockquote> <p>along with the previous error message. This could mean that <code>&amp;</code> operator is not able to create a function pointer out of <code>pAdOne-&gt;Responce1</code>.</p> <p>Does it mean that we can<code>t have a function pointer in some</code>ClassA<code>which could point to a non-static function present in another</code>ClassB`?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload