Calling an objective c method using a function pointer from a c++ method -
i have c++ class needs call objective c method using pointer method. method returns void , takes argument of type 'status' status simple integral enum.
enum status { status_valid = 0, status_invalid, status_unknown };
i have pointer obj-c method set this. pointer method being passed c++ class and
typedef void (*funcptr) (status); funcptr myobjcselectorpointer = (void (*)(status))[self methodforselector:@selector(statuschanged: )]; cppclassinstance->setfuncpointer(myobjcselectorpointer)
myobjcselectorpointer being held on cppclass this:
void cppclass::setfuncpointer(functptr *ptr) { this->funcptr = ptr; }
now somewhere else, when try call method pointed pointer, this:
status s; funcptr(s);
the method gets called correctly, value passed method lost. obj-c method receives random value. when try call method in cpp class using same way, passes , receives correct value.
what doing wrong? can not call objective c method this?
the problem function signature of objective-c methods not simple might think.
in order make object-oriented programming possible, objective-c methods take 2 implicit arguments: id self, sel _cmd
, calling object , selector sent - these need passed. need object call method on, , should store selector too. then, change function type definition correct one:
typedef void (*funcptr) (id, sel, status);
to read: [1], [2] (near part typedef id (*imp)(id, sel, ...)
)
Comments
Post a Comment