home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / sys / next / programm / 5974 < prev    next >
Encoding:
Internet Message Format  |  1992-09-04  |  2.6 KB

  1. Path: sparky!uunet!elroy.jpl.nasa.gov!swrinde!news.dell.com!uudell!pensoft!usenet
  2. From: kari@pencom.com (Kari Karhi)
  3. Newsgroups: comp.sys.next.programmer
  4. Subject: Re: Multiple init: functions defined
  5. Keywords: Objective-C,variable declaration
  6. Message-ID: <1992Sep3.134909.9180@pencom.com>
  7. Date: 3 Sep 92 13:49:09 GMT
  8. References: <Btz6sL.8qC@oceania.com>
  9. Sender: usenet@pencom.com (Usenet Psuedo User)
  10. Reply-To: kari@pencom.com
  11. Organization: Pencom Software
  12. Lines: 72
  13.  
  14. In article <Btz6sL.8qC@oceania.com> jim (Jim Mackraz) writes:
  15. > Jack Gidding writes
  16. > > 
  17. > > Hmmmm..... this is really strange and very undesired behavior from  
  18. > Objective-C.
  19. > > 
  20. > > When I include the header files from two different classes into a third  
  21. > class,  
  22. > > and each of the included classes have a single init: method defined, I get  
  23. > > messages similar to the following:
  24. > > 
  25. > > 
  26. > > main2.m:18: warning: multiple declarations for method `init:'
  27. > > /Clib/RP.h:32: using `-init:(int )pSize'
  28. > > /Clib/RS.h:31: also found `-init:(OBJPTR )s'
  29. > > 
  30. > > 
  31. > > Why is this happening??? Sometimes it tells me that it resolved which init  
  32. to  
  33. > > use, sometimes not, and sometimes it resolves it to the wrong init:.
  34. > > 
  35. > > 
  36. > > The code that has the init method is sililar in form to:
  37. > > 
  38. > >     #import <RP.h>    // Both RP and RS have and init: method
  39. > >     #import <RS.h>
  40. > > 
  41. > >     id x;
  42. > > 
  43. > >     x = [[RP alloc] init:(int) 5];
  44. > > 
  45. > > id is totally dynamic, and thus it really does not know which init to  
  46. use....
  47. > > 
  48. > > So, why is it telling me that it is using one method or another...shouldn't  
  49. > > this be left up until run-time? 
  50. > > 
  51. > > 
  52. > > Am I doing something wrong to cause this?
  53. > Yeah, you're "type overloading" the method "init:".  Don't do that.
  54. > Make one of the "-initInt: (int) intVal" and the other one
  55. >  "-initObj: (OBJPTR) obj"
  56. > .... or something like that.
  57. > > Thanks,
  58. > > 
  59. > > 
  60. > > Jack Gidding
  61. > > jack@richsun.cpg.trs.reuter.com
  62. > -- 
  63. > Jim Mackraz
  64. > jim@oceania.com
  65.  
  66. Another way is to declare the variable x as
  67.             RP *x;
  68. This gives enough clues to the compiler to avoid the warning messages and you  
  69. are, after all, initializing an instance of class RP.  If x can also be  
  70. something else, then you can not use this way. You can also try making the two  
  71. init methods have identical declarations, ie. same argument names and types and  
  72. same return types. Then the compiler will not see any differences and will be  
  73. happy.  In your case that may not be possible either.  In that case renaming  
  74. the method name may be the only solution (outside of just living with the  
  75. warning messages).
  76.  
  77. Cheers,
  78. Kari Karhi
  79. Pencom Software
  80. (512)343-1111
  81. kari@pencom.com
  82.