orr@cs.glasgow.ac.uk (Fraser Orr) (09/02/88)
There is probably a very simple answer to this question, unfortunately I don't know it! I'm trying to create an object with, as some of it's private data members, other objects. Now these data members are objects with constructors, as is the object containing it. Unfortunately this doesn't seem to be allowed (data members may not have constructors). As an example ... class a { int b ; public: a ( int c) { b=c ; } ; } ; class d { a NeedsCtor (1) ; // This line is illegal public: d (int e ) { ... } ; } ; Is it necessary to have a constructor with no parameters for every object that may be used as a data member? I know that it is also possible to have a pointer to a as the data member, and then do a new in d's constructor, but I thought we had got away from all that tacky stuff when we moved to C++. I'm probably missing something obvious, so I would appreciate any enlightenment. Thanks, ==Fraser Orr ( Dept C.S., Univ. Glasgow, Glasgow, G12 8QQ, UK) UseNet: {uk}!cs.glasgow.ac.uk!orr JANET: orr@uk.ac.glasgow.cs ARPANet(preferred xAtlantic): orr%cs.glasgow.ac.uk@nss.cs.ucl.ac.uk
ark@alice.UUCP (Andrew Koenig) (09/06/88)
In article <1606@crete.cs.glasgow.ac.uk>, orr@cs.glasgow.ac.uk (Fraser Orr) writes: > There is probably a very simple answer to this question, unfortunately > I don't know it! The answer is fairly simple, but you shouldn't feel embarrassed -- it's one of the most commonly misunderstood parts of C++. > I'm trying to create an object with, as some of it's private > data members, other objects. Now these data members are objects > with constructors, as is the object containing it. > Unfortunately this doesn't seem to be allowed (data members may not > have constructors). It is allowed, but you have to be careful how you ask for it. > class a { > int b ; > public: > a ( int c) > { > b=c ; > } ; > } ; You have just declared `a' as a class with a constructor that requires an argument. That is, I can say a x(7); but I can't say a y; // illegal; a() undefined > class d { > a NeedsCtor (1) ; // This line is illegal > public: > d (int e ) > { > ... > } ; > } ; If you want to say that an object of class `d' has a member called `NeedsCtor' that is initialized to 1, do it this way: class d { a NeedsCtor; // This line is legal, if ... public: d (int e): NeedsCtor(1) // this line appears as shown. { ... } ; } ; > Is it necessary to have a constructor with no parameters for every > object that may be used as a data member? No. It is necessary to suuply an initial value for every such object, though. The syntax for doing so is as shown above: in each constructor for the class `d' (the class with the data member that needs initialization) you give the initial values for the relevant data members in the constructor(s) for that class. You can do this to supply an initial value for any data member with a constructor, but it's required for any data member without an empty constructor. You do need an empty constructor for a class if you want to create an array of objects of that class, though. > I'm probably missing something obvious, so I would appreciate any > enlightenment. Again, this is nothing to be ashamed of -- while it's not exactly profound, most C++ programmers miss it the first time around. -- --Andrew Koenig ark@europa.att.com