Article View: comp.std.c++
Article #3524Re: Calling X(int) from X()'s init list
From: Etay Bogner
Date: Tue, 27 Feb 1996 18:41
Date: Tue, 27 Feb 1996 18:41
73 lines
2990 bytes
2990 bytes
In article <4gsa2r$gjm@sdaw04.seinf.abb.se>, alindbac@sw.seisy.abb.se wrote:
<< In article <4gognf$et@news.bridge.net>,
<< David Byrden <100101.2547@compuserve.com> wrote:
<< ><<<<<<<
<< >
<< >Why is it not allowed to delegate the initialisation to another
<< >constructor
<< >
<< >class X
<< >{
<< >public:
<< > X (int i) : i_(i) {}
<< >
<< > X () : X(34) {} // Not allowed
<< >>>>>>>>>>
<< >
<< >
<< > Why not use X (int i = 34 ) : i_(i) {}
<<
<< >[ I think he was concerned with the more general case of a complicated
<< > initialization (not assignment) that must be repeated for each
<< > constructor. -sdc, moderator
<< >]
<<
<< Well, one could use a similar thing using a member function:
<<
<< X (int i) { init(i); }
<< X () { init(34); }
<<
<< where init is a member function for class X.
<<
Not very useful for const or reference data members ( here, useful ==
can't be done without explicitly initializing them, but than you "init"
will do only half the work )
My inclination, although I rarly use const or reference data members, will
be to wrap those data members in an internal structure with it's own
constructor :
class A_Class_With_A_Const_Or_Refs {
A_Class_With_A_Const_Or_Refs(long& l);
struct internal_Data {
long& mLong;
const long mClassID; // when there was no RTTI, for instance
internal_Data(long& inLong, const long inID);
} mInternalData;
};
A_Class_With_A_Const_Or_Refs::A_Class_With_A_Const_Or_Refs(long& l) :
mInternalData(l, 'MYID') {}
and so on.
In my opinion, those cases will be very rare.
--
-- Etay Bogner
-- Etay_Bogner@mail.stil.scitex.com
-- Scitex Corp.
-- Israel.
--
-- There are two rules for success in life:
-- Rule 1: Don't tell people everything you know.
--
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Technical Details
Message-ID:
<Etay_Bogner-2702961023450001@metay.stil.scitex.com>Path:
arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austernReferences:
<4gnush$phg@news.kth.se> <4gognf$et@news.bridge.net> <4gsa2r$gjm@sdaw04.seinf.abb.se>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern From: Etay_Bogner@mail.stil.scitex.com (Etay Bogner) Newsgroups: comp.std.c++ Subject: Re: Calling X(int) from X()'s init list Date: 27 Feb 1996 10:41:37 PST Organization: Scitex Corp. Approved: austern@isolde.mti.sgi.com Message-ID: <Etay_Bogner-2702961023450001@metay.stil.scitex.com> References: <4gnush$phg@news.kth.se> <4gognf$et@news.bridge.net> <4gsa2r$gjm@sdaw04.seinf.abb.se> NNTP-Posting-Host: isolde.mti.sgi.com X-Original-Date: Tue, 27 Feb 1996 10:23:45 +0200 X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBMTNQd0y4NqrwXLNJAQGtkwH+JQv2cVwFX6UIh4pdSQXNKAcVaorvhYzc nh9rXZ8V8oVO1jgKKbnz8hGxyI97NJ+C+MUR+5izcQTJPNI/BfGW5w== =EJ7w Originator: austern@isolde.mti.sgi.com