home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / sys / next / programm / 6051 < prev    next >
Encoding:
Text File  |  1992-09-08  |  2.8 KB  |  75 lines

  1. Newsgroups: comp.sys.next.programmer
  2. Path: sparky!uunet!psinntp!afs!greg
  3. From: greg@afs.com (Gregory H. Anderson)
  4. Subject: Re: Multiple init: functions defined
  5. Message-ID: <1992Sep6.155059.13349@afs.com>
  6. Sender: greg@afs.com
  7. Reply-To: greg@afs.com
  8. References: <3575@richsun.cpg.trs.reuter.com>
  9. Date: Sun, 6 Sep 1992 15:50:59 GMT
  10. Lines: 63
  11.  
  12. In article <3575@richsun.cpg.trs.reuter.com>  
  13. jack@richsun.cpg.trs.reuter.com (Jack Gidding) writes:
  14. > When I include the header files from two different classes into a third 
  15. > class, and each of the included classes have a single init: method 
  16. > defined, I get messages similar to the following:
  17. > main2.m:18: warning: multiple declarations for method `init:'
  18. > /Clib/RP.h:32: using `-init:(int )pSize'
  19. > /Clib/RS.h:31: also found `-init:(OBJPTR )s'
  20. > Why is this happening??? Sometimes it tells me that it resolved which 
  21. > init to use, sometimes not, and sometimes it resolves it to the wrong 
  22. > init:.
  23. > So, why is it telling me that it is using one method or another...
  24. > shouldn't this be left up until run-time? 
  25.  
  26. You ARE doing someting undesirable as far as the Obj-C runtime system is  
  27. concerned. A selector name, like a C function, should only have one set of  
  28. argument types. [Think about it: you wouldn't write two C functions with  
  29. the same name, but different arguments, even if they were static to the  
  30. module in which they appeared. Well, you might, but you couldn't get a job  
  31. with my firm. 8^)]
  32.  
  33. You ARE allowed to EXTEND a method's arguments. For example, you can have
  34.  
  35. - init;
  36. - init:sender;
  37. - init:sender forSize:(int)aSize;
  38.  
  39. because the selector's complete name is everything that comes before the  
  40. colons, plus the colons themselves. In my example, the selectors are  
  41. "init", "init:", and "init:forSize:", as you will recognize if you have  
  42. ever set a breakpoint on complex methods in gdb.
  43.  
  44. So the bottom line is that you should have more than one init name if you  
  45. have different argument types. IMHO, that's preferred anyway. All through  
  46. the appkit, "init" takes no arguments. I wouldn't mess with that  
  47. assumption. Instead, the unadorned "init" chooses reasonable defaults and  
  48. then passes control to the "designated initializer." All True NeXTSTEP  
  49. Gods write classes this way. 8^) One last example:
  50.  
  51. - init
  52.     {
  53.     NXRect defaultRect = {{0.0, 0.0.}, {100.0, 150.0}};
  54.     return [self initFrame:&defaultRect];
  55.     }
  56.  
  57. - initFrame:(NXRect *)aRect   /* this is the "designated initializer" */
  58.     {
  59.     /* do some stuff here */
  60.     return self;
  61.     }
  62.  
  63. I agree, on the surface this can be a nuisance. I have come to like  
  64. specific init names, because they are self-documenting.
  65.  
  66. --
  67. Gregory H. Anderson          | "We're very tolerant around here,
  68. Benevolent Dictator-for-Life |  being only amateurs ourselves."
  69. Anderson Financial Systems   | - Tortoise (Godel, Escher, Bach)
  70. greg@afs.com  (Nextmail OK)  | 
  71.