home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!haven.umd.edu!darwin.sura.net!gatech!enterpoop.mit.edu!eru.mt.luth.se!hagbard!loglule!jbn
- From: jbn@lulea.trab.se (Johan Bengtsson)
- Newsgroups: comp.lang.c++
- Subject: Re: why `int X: :X()' ?
- Message-ID: <5516@miramon.lulea.trab.se>
- Date: 12 Jan 93 10:13:31 GMT
- References: <848@ulogic.UUCP>
- Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
- Lines: 57
- X-Newsreader: TIN [version 1.1 + PL8]
-
- Richard M. Hartman (hartman@ulogic.UUCP) wrote:
- : In article <5501@miramon.lulea.trab.se> jbn@lulea.trab.se (Johan Bengtsson) writes:
- : >Matt Wette (mwette@csi.jpl.nasa.gov) wrote:
- : >
- : >: Why are constructor and destructor functions usually declared to return
- : >: ints when the usage typically warrents a `void' declaration.
- : >
- : >The "default int" rule does not apply to constructors and destructors.
- : >You can't return an "int" from those functions. You can't explicitly
- : >mark them as procedures (void) either.
- : >
- : >I'd say that "default void" (as for constructors and destructors)
- : >makes more sense than "default int". Having no default type at all
- : >makes the most sense, avoiding compiler and programmer confusion.
-
- : I'd say you are misleading yourself. The best way (IMHO) to think
- : of constructors is returning the type for which they are a constructor.
-
- There are two useful ways to think of constructors, your way and my way.
-
- My way (implementation side):
- "a constructor has no return value"
- Your way (usage side):
- "a call of a constructor returns an object"
-
- This is similar to the duality of overloaded "new" operators,
- from the implementation side it is a function size_t --> void*.
- From the usage side it is a different beast, returning a typed
- pointer.
-
- In both cases the compiler inserts a bit of extra code when
- the function is used.
-
- : but if you start thinking of
-
- : void classType::classType() {}
-
- : Then declarations such as:
-
- : classType classInstance = classType();
-
- This is an explicit constructor call (see the ARM, p266,267).
- You are not calling the classType::classType() function directly.
- The compiler inserts code to allocate space for a temporary "classType"
- object, and then calls classType::classType() function (which has
- no return value, but instead relies on the "this" pointer for
- access to the object to be constructed).
-
- You cannot say "return classType(somevalue)" inside
- classType::classType() (which would be the consequence
- if constructors had an implied non-void return type).
-
- --
- --------------------------------------------------------------------------
- | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden |
- | Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490 |
- --------------------------------------------------------------------------
-