Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!hplabs!hp-pcd!hplsla!jima From: jima@hplsla.HP.COM (Jim Adcock) Newsgroups: comp.lang.c++ Subject: Re: Where does this construct come from? Message-ID: <6590223@hplsla.HP.COM> Date: 1 Aug 89 17:55:22 GMT References: <2701@hsinchu.sw.mcc.com> Organization: HP Lake Stevens, WA Lines: 44 >class multi_v { >public: > vect a, b, c; > multi_v(int i):a(i), b(i), c(i) { } > // ^^^^^^^^^^^^^^^^^ >}; Its in Lippman somewhere -- that's where I first noticed it. If you want to allow constant objects to be created, this is the technique you should use. The comma part is the initialization section. The brace part is construction: class vect { public: int i1, i2, i3; vect(int i) : i1(i), i2(i), i3(i) { } vect& operator=(int i) {i1=i; i2=i; i3=i; return *this;} }; class multi_v { public: vect a, b, c; multi_v(int i):a(i), b(i), c(i) { } }; const multi_v mv(5); //multi_v works class bogus_v { public: vect a, b, c; bogus_v(int i) {a=i; b=i; c=i;} }; const bogus_v bv(5); //but bogus bombs Note: the comma initializer section works on built-in types too, not just user defined classes. In general, one is better off to move as much as possible out of the constructor section and into the initializer section. The initializer section can contain coercions and the like, so it is not limited to trivial initializations.