Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I use method pointers in c++ alike delegates in c#?
    text
    copied!<p>I would like to make a class' (let's call it X for convenience) method be able to use a pointer to its own methods in cases where I would be able to extract code that differs only by the method called.</p> <p>for example, having a few blocks like:</p> <pre><code>a(); b(); c(); MehtodCall(someResultOfabc); d(); e(); </code></pre> <p>I would like to extract a method that would just use different methods passed as a pointer:</p> <p>so I would like something alike:</p> <pre><code>void X::ExtractedMethod( void(X::*MethodCallPointer)(TypeOfABCResult) ){ a(); b(); c(); this-&gt;*MethodCallPointer(someResultOfabc); d(); e(); } </code></pre> <p>and use this very function in such manner:</p> <pre><code>void X::DoSomethingA(){ ExtractedMethod(&amp;X::ADoer); } void X::DoSomethingB(){ ExtractedMethod(&amp;X::BDoer); } </code></pre> <p>and os on...</p> <p>Unfortunately I am stuck here, because I get a compile error, that the method pointer doesn't evaluate to a method call which takes one argument.</p> <p>I start to wander if the reason would be that X is yet an incomplete type in this declaration ?</p> <p>I would be happy if someone would help me by telling how to do this, or why isn't that possible.</p> <p>What I really, really DON'T want is to get some workarounds, like using std::function in the signature and a lambda, or using a pointer to a static methods that takes X* and invoke specific methods that I need, nor making switch statements and a flag, nor instead of that switch statement use an interface, implement the strategy patern and use static instances of strategies as a parameter ;) those aren't all workaround for this case, but I stil don't want to read about other workarounds. The sole thing that interests me is "is that possible, and how to do it" in this very way.</p>
 

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