I have a C++ class 'Expression
' with a method I'd like to use in my Objective-C class 'GraphVC
'.
class Expression {
double evaluate(double);
}
And my Objective-C class:
@implementation GraphVC : UIViewController {
- (void)plot:(double(*)(double))f;
@end
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 functional.h
. What's the best way to use my C++ method from Objective-C?
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 mem_fun from , but I wasn't able to get code compiling this way. I also had trouble using bind1st to bind the this
pointer.
- Writing an Objective-C wrapper is a possibility, but I'd rather use the already existing
evaluate()
function, and I don't want to break the clean separation between the backend and the iPhone GUI classes. - I can't have a global expression or use a static method (I need to plot arbitrary
Expression
instances.
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 <functional>
to turn member functions into pointers I can use in an Objective-C object, or should I use an Objective-C wrapper?