Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing a C++ method to an Objective-C method
    text
    copied!<p>I have a C++ class '<code>Expression</code>' with a method I'd like to use in my Objective-C class '<code>GraphVC</code>'.</p> <pre><code>class Expression { double evaluate(double); } </code></pre> <p>And my Objective-C class:</p> <pre><code>@implementation GraphVC : UIViewController { - (void)plot:(double(*)(double))f; @end </code></pre> <p>I thought that it would be easiest to pass around function pointers that take a double and return a double, as opposed to C++ objects, but I haven't had much success using <code>functional.h</code>. What's the best way to use my C++ method from Objective-C?</p> <p>EDIT: Thanks for your quick responses. Allow me to elaborate a bit... I have a backend written in C++ where I manipulate objects of type Expression. There's subclasses for rational, polynomial, monomial, etc. My initial idea was to use <a href="http://www.cplusplus.com/reference/std/functional/mem_fun/" rel="nofollow noreferrer">mem_fun</a> from , but I wasn't able to get code compiling this way. I also had trouble using bind1st to bind the <code>this</code> pointer.</p> <ul> <li>Writing an Objective-C wrapper is a possibility, but I'd rather use the already existing <code>evaluate()</code> function, and I don't want to break the clean separation between the backend and the iPhone GUI classes.</li> <li>I can't have a global expression or use a static method (I need to plot arbitrary <code>Expression</code> instances.</li> </ul> <p>I should have more explicitly stated that I need to pass a C++ member function (not a static function or existing C function) to an Objective-C object. Has anyone had luck using C++'s <code>&lt;functional&gt;</code> to turn member functions into pointers I can use in an Objective-C object, or should I use an Objective-C wrapper? </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