home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18993 < prev    next >
Encoding:
Internet Message Format  |  1993-01-12  |  2.7 KB

  1. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!gatech!enterpoop.mit.edu!eru.mt.luth.se!hagbard!loglule!jbn
  2. From: jbn@lulea.trab.se (Johan Bengtsson)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: why `int X: :X()' ?
  5. Message-ID: <5516@miramon.lulea.trab.se>
  6. Date: 12 Jan 93 10:13:31 GMT
  7. References: <848@ulogic.UUCP>
  8. Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
  9. Lines: 57
  10. X-Newsreader: TIN [version 1.1 + PL8]
  11.  
  12. Richard M. Hartman (hartman@ulogic.UUCP) wrote:
  13. : In article <5501@miramon.lulea.trab.se> jbn@lulea.trab.se (Johan Bengtsson) writes:
  14. : >Matt Wette (mwette@csi.jpl.nasa.gov) wrote:
  15. : >
  16. : >: Why are constructor and destructor functions usually declared to return
  17. : >: ints when the usage typically warrents a `void' declaration.
  18. : >
  19. : >The "default int" rule does not apply to constructors and destructors.
  20. : >You can't return an "int" from those functions.  You can't explicitly
  21. : >mark them as procedures (void) either.
  22. : >
  23. : >I'd say that "default void" (as for constructors and destructors)
  24. : >makes more sense than "default int".  Having no default type at all
  25. : >makes the most sense, avoiding compiler and programmer confusion.
  26.  
  27. : I'd say you are misleading yourself.  The best way (IMHO) to think
  28. : of constructors is returning the type for which they are a constructor.
  29.  
  30. There are two useful ways to think of constructors, your way and my way.
  31.  
  32. My way (implementation side):
  33.     "a constructor has no return value"
  34. Your way (usage side):
  35.     "a call of a constructor returns an object"
  36.  
  37. This is similar to the duality of overloaded "new" operators,
  38. from the implementation side it is a function size_t --> void*.
  39. From the usage side it is a different beast, returning a typed
  40. pointer.
  41.  
  42. In both cases the compiler inserts a bit of extra code when
  43. the function is used.
  44.  
  45. : but if you start thinking of
  46.  
  47. :     void classType::classType() {}
  48.  
  49. : Then declarations such as:
  50.  
  51. :     classType classInstance = classType();
  52.  
  53. This is an explicit constructor call (see the ARM, p266,267).
  54. You are not calling the classType::classType() function directly.
  55. The compiler inserts code to allocate space for a temporary "classType"
  56. object, and then calls classType::classType() function (which has
  57. no return value, but instead relies on the "this" pointer for
  58. access to the object to be constructed).
  59.  
  60. You cannot say "return classType(somevalue)" inside
  61. classType::classType() (which would be the consequence
  62. if constructors had an implied non-void return type).
  63.  
  64. -- 
  65. --------------------------------------------------------------------------
  66. | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
  67. | Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490  |
  68. --------------------------------------------------------------------------
  69.