Re: method caller mixup
Re: method caller mixup
- Subject: Re: method caller mixup
- From: James Maxwell <email@hidden>
- Date: Sat, 17 Apr 2010 13:24:49 -0700
Thanks folks,
I actually did wind up type casting the alloc, as recommended. Sorry I didn't post when I found the solution.
I guess I've just never come across this before, which seems kind of strange... but true.
cheers,
J.
On 2010-04-17, at 12:42 PM, Paul Sanders wrote:
> > It's not particularly tricky. +alloc returns an id, and Objective-C doesn't have operand-overloaded methods.
> > There is nothing in your alloc/init expression that tells the _compiler_ which of the differently-defined initWithParent:
> > methods you intend to use, so it picks one in some undefined way. In this case, it picked the one you didn't intend.
> >
> > You can force the choice by casting the result of +alloc to the intended type:
> >
> > [ (HSMM_TrieNode *)[HSMM_TrieNode alloc] initWithParent: aNode ]
>
> -Wstrict-selector-match would have reported the ambiguity. Otherwise, the following code compiles without complaint (this is on gcc 4.0; on gcc 4.2 the warning flag might be on by default):
>
> @interface Class1 : NSObject
> - (id) initWithScalar: (int) i;
> @end
>
> @interface Class2 : NSObject
> - (id) initWithScalar: (float) f;
> @end
>
> void test (void)
> {
> Class1 *o1 = [[Class1 alloc] initWithScalar: 1];
> Class1 *o2 = [[Class1 alloc] initWithScalar: 1.0f];
> Class2 *o3 = [[Class2 alloc] initWithScalar: 1];
> Class2 *o4 = [[Class2 alloc] initWithScalar: 1.0f];
> }
>
> However, two of these four init method invocations will get a parameter in a form (int vs float) they don't expect since the compiler doesn't have enough information to perform the expected implicit type conversion across the call, and that is not good.
> As for the scope and interpretation of method names, this might be worth a look if you haven't already:
>
> http://developer.apple.com/Mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocSelectors.html#//apple_ref/doc/uid/TP30001163-CH23-SW1
>
> Paul Sanders.
>
James B Maxwell
Composer/Doctoral Student
School for the Contemporary Arts (SCA)
School for Interactive Arts + Technology (SIAT)
Simon Fraser University
email@hidden
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden