home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13325 < prev    next >
Encoding:
Text File  |  1992-09-07  |  2.2 KB  |  56 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!mcsun!Germany.EU.net!ira.uka.de!slsvaat!us-es.sel.de!kanze
  3. From: kanze@us-es.sel.de (James Kanze)
  4. Subject: Re: constructor call during initialization of same class
  5. Message-ID: <1992Aug28.141327.11921@us-es.sel.de>
  6. Sender: news@us-es.sel.de
  7. Organization: SEL-Alcatel LTS Dept. US/ES
  8. References: <1992Aug23.030355.23047@athena.mit.edu> <1992Aug23.154530.1131@taumet.com> <1992Aug27.182902.2022@us-es.sel.de>
  9. Date: Fri, 28 Aug 92 14:13:27 GMT
  10. Lines: 44
  11.  
  12. I'm reposting the following, since I notice that I forgot to correct the distribution
  13. in the initial post.  (We default to a local distribution here.)
  14.  
  15. In article <1992Aug23.154530.1131@taumet.com>, steve@taumet.com (Steve Clamage) writes:
  16. |> casadei@verona.mit.edu (Stefano Casadei) writes:
  17. |> 
  18. |> >Is it possible to define a class constructor which invokes another
  19. |> >constructor of the same class during the initialization phase (i.e. between
  20. |> >the : and the body) ? If not, why is such a simple feature not implemented
  21. |> >in the language ?
  22.  
  23.      [...]
  24.  
  25. |> I am guessing that you want to share some code by having several
  26. |> constructors call a "basic" constructor.  You can't do that with
  27. |> a construcutor.  You can write an initialization function which all
  28. |> constructors call.  This is equivalent to what I think you want:
  29. |>     class foo {
  30. |>     public:
  31. |>         foo();
  32. |>         foo(int);
  33. |>         foo(char*);
  34. |>     private:
  35. |>         init();
  36. |>     }
  37. |>     foo::foo() { ... init(); ... }
  38. |>     foo::foo(int i) { ... init(); ... }
  39. |>     foo::foo(char* p) { ... init(); ... }
  40.  
  41. Actually, I think you *can* call a constructor (for the same object)
  42. within a constructor.  If not, what does the following code do:
  43.  
  44.     foo::foo(int i) { ... ; new (this) foo ; ... }
  45.  
  46. On the other hand, I don't think you really want to.  To begin with,
  47. the syntax is, to say the least , unpleasant.  And what happens if
  48. foo is a derived class: the constructor for the base class gets called
  49. twice, without an intervening destructor.  That could lead to some
  50. unpleasantness too.
  51. --
  52. James Kanze                       GABI Software, Sarl.
  53. email: kanze@us-es.sel.de         8 rue du Faisan
  54.                                   67000 Strasbourg
  55.                                   France
  56.