Subj : Re: class member initialisation To : borland.public.cpp.borlandcpp From : "Ed Mulroy [TeamB]" Date : Fri Oct 17 2003 10:11 am This initializes things without complaint. ---------------------------- class Apple { private: int i1; int i2; public: Apple(int ii1,int ii2) : i1(ii1), i2(ii2) {} // added default constructor so that // the FruitBox constructor which does not // have code to construct Apple will work Apple() : i1(0), i2(0) {} }; class FruitBox { private: char c1; Apple a1; FruitBox(char c); FruitBox(double s) : a1(s, s+ 5), c1('J') {} }; FruitBox::FruitBox(char c) : c1(c) { if (c > 'X') a1 = Apple(c + 45, c -12); else a1 = Apple(c, c); } ---------------------------- Note the following: Each constructor must have a unique set of calling arguments. In the code of the body of a constructor, in what follows after the initialization list of a constructor, any instances of a class contained in the class being constructed have themselves already been constructed. If the class instances contained in another class as member variables are not specificly constructed in the initialization list of the class being constructed then the default constructors for those instances (the constructors which have no calling arguments) will be called. Therefore you must have a default constructor. (If the class definitions of those member variables have no constructors for then the compiler will cobble one up for you but it may not do what you wanted.) In the FruitBox::FruitBox(char c) constructor I did an assignment of an instance of apple to the variable 'a1'. There is no copy constructor declared for the class so the compiler creates one copying element by element. If the class Apple had more complex data members such as pointers to arrays (including char* pointers to char strings) or other classes that default copying may not do what you wanted. It would be best to provide a copy constructor, a constructor with the following signature: Apple(const & Apple some_name); .. Ed > David Currie wrote in message > news:3F8EA387.FC0EF6B@webspinning.com.au... > Consider the following generalisation > > class Apple > { > private: > int i1; // data members > int i2; > public: > Apple(int ii1,int ii2); // Constructor > > // Destructors + methods etc > } > > class FruitBox > { > private: > char c1; > Apple a1; > > FruitBox (say) (char c); // Constructor 1 > FruitBox(char c) : a1(value1,value2); // Constructor 2 // example > FruitBox(something s) // Constructor 3 // example > > // Destructors + methods etc > } > > FruitBox::FruitBox(char c) // Constructor 1 > { > /* ... make a decision (perhaps based on char c) > how to construct Apple a1 */ > > Now how do i construct Apple a1 here ? > } > > ps : I know using constructor 2 the compiler can construct Apple for > me > > but in general i dont know how to construct Apple until i am already > in the function > > 1) Apple a1 exists (its a part of the FruitBox object) > > can i construct Apple a1 here, if so how > > i know i can redesign methods here (say default Apple > constructor + setters) > or say Apple localApple(value1,value2); > (this->)a1 = localApple; > requiring operator= (assignment operator + copy methods etc) > and to a degree this is duplicating effort. > but in general this is not desired (for many reasons ...) > > > how do i initialise it directly ? is it initialisable by reference > ? this->a1(value1,value2) ? > can a correct syntax construct Apple here ? > if so what is it > > 3) For that matter, consider constructor 3 > > can constructor 3 (after some code sequence ...) construct itself > using constructor 1 ? > if so what is the appropriate syntax for one constructor to call > another constructor? > > 4) Is all this a design limitation of C++? > > > any responses would be greatly appreciated. > } .