home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!gatech!bloom-beacon!bloom-picayune.mit.edu!verona.mit.edu!casadei
- From: casadei@verona.mit.edu (Stefano Casadei)
- Subject: Re: constructor call during initialization of same class
- Message-ID: <1992Aug23.191938.2082@athena.mit.edu>
- Sender: news@athena.mit.edu (News system)
- Nntp-Posting-Host: verona.mit.edu
- Organization: Massachusetts Institute of Technology
- References: <1992Aug23.030355.23047@athena.mit.edu> <1992Aug23.154530.1131@taumet.com>
- Date: Sun, 23 Aug 1992 19:19:38 GMT
- Lines: 77
-
- In article <1992Aug23.154530.1131@taumet.com> steve@taumet.com (Steve Clamage) writes:
- >casadei@verona.mit.edu (Stefano Casadei) writes:
- >
- >>Is it possible to define a class constructor which invokes another
- >>constructor of the same class during the initialization phase (i.e. between
- >>the : and the body) ? If not, why is such a simple feature not implemented
- >>in the language ?
-
- .....
- >I am guessing that you want to share some code by having several
- >constructors call a "basic" constructor. You can't do that with
- >a construcutor. You can write an initialization function which all
- >constructors call. This is equivalent to what I think you want:
- > class foo {
- > public:
- > foo();
- > foo(int);
- > foo(char*);
- > private:
- > init();
- > }
- > foo::foo() { ... init(); ... }
- > foo::foo(int i) { ... init(); ... }
- > foo::foo(char* p) { ... init(); ... }
- >--
- >
- >Steve Clamage, TauMetric Corp, steve@taumet.com
- >Vice Chair, ANSI C++ Committee, X3J16
-
-
- Thanks for your reply. (I tried to reply you by mail but my your machine
- was unknown to mine).
-
- I thought that by constructing the object during the assignement phase
- (i.e. within the body of the constructor) is less time-inefficient than
- by doing so during the initialization phase (right after the ':').
- For instance in my particular case, if I do the following:
-
- ----------------- cut here ------------------
-
- class picture { ... picture(pic,wid,hei,row,col) ... }
-
- class lmodel {
- public:
- lmodel (picture);
- lmodel (picture pic, int row, int col, int wid, int hei);
- private:
- ....many member objects.........
- };
-
- lmodel::lmodel(picture) {...}
- lmodel::lmodel(picture pic, int row, int col, int wid, int hei)) { *this=lmodel(picture(pic,wid,hei,row,col)); }
-
-
- ----------------- cut here ------------------
-
-
- then I would expect that the source line:
-
- lmodel::lmodel(picture pic, int row, int col, int wid, int hei)) { *this=lmodel(picture(pic,wid,hei,row,col)); }
-
- requires three initialization of the member objects of the class lmodel:
- one for the initialization proper;
- another for the construction of the RHS of the assignment;
- and another for the assignment.
-
- On the other hand, if the following line were accepted:
-
- lmodel::lmodel(picture pic, int row, int col, int wid, int hei)) : (*this)(picture(pic,wid,hei,row,col)) {}
-
- then only one 'scan' would suffice.
- I saw an argument like this in Lippman's book, I don't remember where exactly.
- Is it right ?
-
- Thanks.
-
- Stefano
-