• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
init method ambiguity
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

init method ambiguity


  • Subject: init method ambiguity
  • From: Jonny Taylor <email@hidden>
  • Date: Thu, 4 Nov 2010 12:23:03 +0000

Apologies for a very basic question, but unfortunately one that I don't really know what keywords to search for an answer about.

Suppose I have two separate classes with init methods that have the same name, but which take different types. MyClassA has:
-(id)initForCamera:(QICamera*)cam
and MyClassB has:
-(id)initForCamera:(PSCamera*)cam

Then, when I do something like
QICamera *cam;
[[MyClassA alloc] initForCamera:cam];
I sometimes encounter compiler warnings saying for example:
warning: incompatible Objective-C types 'PSCamera*', expected 'QICamera*' when passing argument 1 of 'initForCamera:' from distinct Objective-C type

If instead I write:
[(MyClassA*)[MyClassA alloc] initForCamera:cam];
then I do not get a warning. This leads me to believe that what I am writing is not actually doing anything wrong, and the compiler warning is associated with the fact that [MyClassA alloc] returns a plain id leaving the compiler to do some deducing about what the type actually is. This, combined with the confusion of having two methods (for different classes) with the same name but different parameter types, seems to be confusing things.

One workaround is to include explicit casts like I have shown. However this leaves me wondering whether it goes against convention and/or recommendations to have two init methods with the same name but different parameter types, even for two unrelated different classes (hard to enforce - one could imagine completely different codebases colliding by chance in this respect). Can anybody comment on this?

Thanks
Jonny

_______________________________________________

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

  • Follow-Ups:
    • Re: init method ambiguity
      • From: Sherm Pendley <email@hidden>
    • Re: init method ambiguity
      • From: Fritz Anderson <email@hidden>
  • Prev by Date: Re: Set Icon For Document-Based Files
  • Next by Date: Re: init method ambiguity
  • Previous by thread: Re: Setting (not saving) NSPersistentDocument Metadata changes file Modification Date
  • Next by thread: Re: init method ambiguity
  • Index(es):
    • Date
    • Thread