Re: calling method via IMP that returns non id type - compiler warning
Re: calling method via IMP that returns non id type - compiler warning
- Subject: Re: calling method via IMP that returns non id type - compiler warning
- From: "b.bum" <email@hidden>
- Date: Sun, 15 Feb 2004 10:59:39 -0800
On Feb 15, 2004, at 8:59 AM, Ben Dougall wrote:
i've used an IMP to call a method, and that method returns a
typedeffed type that isn't an object pointer -- not an id type that
is. it all works fine apart from it gives a warning. if i cast the
returned value from the IMP call the warning goes away -- but is that
the way to fix that?
so at the moment i'm using the IMP like so:
typedef u_int8_t *MyType;
...
MyType result = theMethodIMP(object, nil, value);
and the method that the IMP stands for/points to is declared like this:
- (MyType)theMethod:(unsigned)value;
the above IMP call line gives this warning:
warning: assignment from incompatible pointer type
but changing the IMP call line to:
MyType result = (MyType)theMethodIMP(object, nil, value);
stops the warning. is that how i should stop the warning, or is there
some other way i should be using?
Most likely, you haven't declared the type of theMethodIMP
appropriately. It shouldn't just be IMP, but needs to be specific to
the signature of the method you are invoking.
I.e. something like.....
MyType (*theMethodIMP)(id, SEL, unsigned);
theMethodIMP = ... instanceMethodForSelector: ...
result = theMethodIMP(self, @selector(theMethod:), value)
(all typed in email -- you'll have to figure out the exact syntax for
the decl if that isn't right).
BUT two issues:
- you really should pass the SEL of the method in the IMP call. If you
don't, _cmd will be undefined within the body of the method. While
that may not cause a problem now, it will if you ever happen to use a
macro that uses it (NSASSERT()) or someone overrides and provides an
implementation that relies upon a defined _cmd.
- why are you doing this in the first place? Is there some reason not
to use NSInvocation or just invoke the method as you normally would?
Generally, calling the IMP directly is a performance optimization only
used in extreme cases. Have you identified a performance hit that
warrants such a solution? (You might have -- I don't know -- but I do
know that just about every time this has ever come up on the list(s),
it has been a case of "premature optimization").
b.bum
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.