Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!tdatirv!sarima From: sarima@tdatirv.UUCP (Stanley Friesen) Newsgroups: comp.lang.c++ Subject: Re: Initializing a Member That is a Class Message-ID: <125@tdatirv.UUCP> Date: 4 Feb 91 15:55:57 GMT References: <1991Jan31.194631.3447@persoft.com> Reply-To: sarima@tdatirv.UUCP (Stanley Friesen) Distribution: na Organization: Teradata Corp., Irvine Lines: 36 In article <1991Jan31.194631.3447@persoft.com> 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? Hmm, I rather though the explanation of this in Lippman was quite clear. Essentially you pass arguments to the A constructor from the B and C constructors. The arguments can, of course, be any legal C++ expression involving identifiers that are in scope at the time. This includes the arguments to the B/C constructors. The syntax is as follows: class B { A a_member; public: B(...) :a_member(d, e, f) {.... }; } For instance any of the following would bbe legal: B::B(z) :a_member(1, 2, 3) {...} B::B(z) :a_member(z, z, z+3) {...} B::B(y, z) :a_member(5, y+z, y) {...} and so on. The rules are exactly the same for struct C (because a struct is just a special case of a class). (Note that the sub-constructor arguments go with the *definition* of the outer constructor, not with its declaration in the class definition when the two are different). -- --------------- uunet!tdatirv!sarima (Stanley Friesen)