Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c++ Subject: Re: Initializing a Member That is a Class Message-ID: <580@taumet.com> Date: 4 Feb 91 16:55:07 GMT References: <1991Jan31.194631.3447@persoft.com> Distribution: na Organization: Taumetric Corporation, San Diego Lines: 54 eda@persoft.com (Ed Almasy) writes: >Assuming: > A is a class, with a constructor that requires 3 arguments > B is a class, containing a member of type A > C is a struct, containing a member of type A >My question is: When I create data items of type B or C, where do the >constructor arguments for the member that is of type A come from? A constructor header includes an initialization clause. You may specify initialization of base classes (via their constructors), and of data members. Inside the constructor you specify only assignment, not initialization, of data members. Given class A { public: A(int, int, int); ... }; class B { A a; B(int, int, int); ... }; you would write, for example: B::B(int i, int j, int k) : A(i, j, k) { } This is the same way you pass arguements to base class constructors. This is also the only way you can initialize constant or reference members: class A { ... } a; class C { const int i; A& aref; public: C(); }; C::C() : i(3), aref(a) { } because consider this: C::C() { i = 3; // illegal, i is const aref = ... // aref is uninitialized, so assignment through it // won't do anything good. You can't assign TO a ref. } -- Steve Clamage, TauMetric Corp, steve@taumet.com