home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 12780 < prev    next >
Encoding:
Text File  |  1992-08-23  |  2.9 KB  |  90 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!gatech!bloom-beacon!bloom-picayune.mit.edu!verona.mit.edu!casadei
  3. From: casadei@verona.mit.edu (Stefano Casadei)
  4. Subject: Re: constructor call during initialization of same class
  5. Message-ID: <1992Aug23.191938.2082@athena.mit.edu>
  6. Sender: news@athena.mit.edu (News system)
  7. Nntp-Posting-Host: verona.mit.edu
  8. Organization: Massachusetts Institute of Technology
  9. References: <1992Aug23.030355.23047@athena.mit.edu> <1992Aug23.154530.1131@taumet.com>
  10. Date: Sun, 23 Aug 1992 19:19:38 GMT
  11. Lines: 77
  12.  
  13. In article <1992Aug23.154530.1131@taumet.com> steve@taumet.com (Steve Clamage) writes:
  14. >casadei@verona.mit.edu (Stefano Casadei) writes:
  15. >
  16. >>Is it possible to define a class constructor which invokes another
  17. >>constructor of the same class during the initialization phase (i.e. between
  18. >>the : and the body) ? If not, why is such a simple feature not implemented
  19. >>in the language ?
  20.  
  21. .....
  22. >I am guessing that you want to share some code by having several
  23. >constructors call a "basic" constructor.  You can't do that with
  24. >a construcutor.  You can write an initialization function which all
  25. >constructors call.  This is equivalent to what I think you want:
  26. >    class foo {
  27. >    public:
  28. >        foo();
  29. >        foo(int);
  30. >        foo(char*);
  31. >    private:
  32. >        init();
  33. >    }
  34. >    foo::foo() { ... init(); ... }
  35. >    foo::foo(int i) { ... init(); ... }
  36. >    foo::foo(char* p) { ... init(); ... }
  37. >-- 
  38. >
  39. >Steve Clamage, TauMetric Corp, steve@taumet.com
  40. >Vice Chair, ANSI C++ Committee, X3J16
  41.  
  42.  
  43. Thanks for your reply. (I tried to reply you by mail but my your machine
  44. was unknown to mine).
  45.  
  46. I thought that by constructing the object during the assignement phase 
  47. (i.e. within the body of the constructor) is less time-inefficient than
  48. by doing so during the initialization phase (right after the ':').
  49. For instance in my particular case, if I do the following:
  50.  
  51. ----------------- cut here ------------------
  52.  
  53. class picture { ... picture(pic,wid,hei,row,col) ... }
  54.  
  55. class lmodel {
  56. public:
  57.     lmodel (picture);
  58.     lmodel (picture pic, int row, int col, int wid, int hei); 
  59. private:
  60.     ....many member objects.........
  61. };
  62.  
  63. lmodel::lmodel(picture) {...}
  64. lmodel::lmodel(picture pic, int row, int col, int wid, int hei)) {  *this=lmodel(picture(pic,wid,hei,row,col)); }
  65.  
  66.  
  67. ----------------- cut here ------------------
  68.  
  69.  
  70. then I would expect that the source line:
  71.  
  72. lmodel::lmodel(picture pic, int row, int col, int wid, int hei)) {  *this=lmodel(picture(pic,wid,hei,row,col)); }
  73.  
  74. requires three initialization of the member objects of the class lmodel: 
  75. one for the initialization proper;
  76. another for the construction of the RHS of the assignment;
  77. and another for the assignment.
  78.  
  79. On the other hand, if the following line were accepted:
  80.  
  81. lmodel::lmodel(picture pic, int row, int col, int wid, int hei)) :  (*this)(picture(pic,wid,hei,row,col)) {}
  82.  
  83. then only one 'scan' would suffice. 
  84. I saw an argument like this in Lippman's book, I don't remember where exactly.
  85. Is it right ?
  86.  
  87. Thanks.
  88.  
  89. Stefano
  90.