C++ Initialize Class Member Variables of Another Class Type
So I'm wondering how I can make a constructor for a class contain other class members.
Here's my constructor: Course::Course() { courseCode = ""; section = ""; DaysOfWeek* daysMeets = new DaysOfWeek(); TimeInterval* timeMeets = new TimeInterval(); instructorName = ""; } My private member variables for Course is: string courseCode; string section; DaysOfWeek daysMeets; TimeInterval timeMeets; string instructorName; Will my daysMeets private variable be initialized (which is what I want) or will it just create it's own NEW DaysOfWeek object that overrides my private member variable. I have a feeling it's the second, so my question is: what is the proper way to initialize a private member variable that is of another class type? Unfortunately I can't test this for some time, and it's really bothering me. I've searched all over for the answer, it compiles of course, but I doubt it's doing what I want. |
Also, this compiles, but I'm not sure if this is what I'm looking for either, but it does seem much more like what I want:
daysMeets = DaysOfWeek(); timeMeets = TimeInterval(); |
daysMeets is a pointer to an object. So what you want is:
Code:
daysMeets = new DaysOfWeeks(); What you had: Code:
DaysOfWeeks * daysMeets = new DaysOfWeeks(); |
@ the OP --
Please be advised that the proper place to initialize class member data is not in the body of the constructor, but prior to it. Code:
class Foo Code:
Course::Course() |
You can also make multiple constructors by overloading them:
Code:
Course::Course() new Course(days, meets); or new Course(days); or new Course(); in case you constructed the other classes already, you can use their data in your constructor. |
Quote:
You have clearly implied that DaysOfWeek and TimeInterval both have default constructors. That means you don't need to initialize daysMeets nor timeMeets. The compiler takes care of that for you, just as it takes care of initializing the strings for you. So your Course constructor doesn't need to do anything at all. Assuming Course is something like Code:
class Course { Code:
Course::Course() {} Code:
Course::Course() : Those lines remind anyone reading your code that the default constructor is used for each of those members. If you leave the line out, the compiler still knows the default constructor is used. If you wanted to use a non default constructor for any one of those members, you would fill in the constructor parameters in the (). Quote:
The compiler inserts a call to the default constructor for each of those members because you didn't specify their construction. Then you specify that a temporary object of the same type be created with another call to the default constructor, then copied over the already initialized member with a call to the assignment operator. (The compiler might optimize away some of that extra nonsense, but you still shouldn't have coded it in the first place). |
Quote:
I currently have something like this in the other constructor: Course::Course(string aCourseCode, string aSection, DaysOfWeek& aDaysMeets, TimeInterval& aTimeMeets, string aInstructorName) { courseCode = aCourseCode; section = aSection; daysMeets = DaysOfWeek(aDaysMeets.get()); timeMeets = TimeInterval(aTimeMeets.getStart(),aTimeMeets.getEnd()); instructorName = aInstructorName; } Quote:
|
Quote:
Code:
Course::Course(string const& aCourseCode, 1) Passing strings by const& is generally better than by value (and definitely better in this example), so I made that change. But this change is independent of the overall change in the structure of the constructor. 2) The sequence of the initialization list between the ) : and the {} should match the sequence that the members are declared in the definition of the class. I don't know what sequence you declared them in, so I just kept the sequence from your version of the constructor. Note the sequence of parameters to your constructor is whatever you want. The compiler does not relate it to the sequence of members initialized from those parameters. 3) I see you passed two objects by & rather than const&. I didn't change that because I don't know whether your get() methods are const methods. But it looks like a situation where the get() methods should have been const and the parameters passed to Course() should have been const&. 4) Your code makes me wonder what DaysOfWeek(aDaysMeets.get()) is doing. It looks like the intent is to copy the object. Is there a good reason that is not done with a direct copy? In your form of the constructor that would be daysMeets = aDaysMeets; In my form of the constructor, that would be daysMeets( aDaysMeets ), 5) As I explained before, your version of the code looks like it would work as long as DaysOfWeek and TimeInterval have default constructors. But your code does a lot of excess work: calling the default constructor that you don't actually want, then making a temporary object with the correct constructor, then copying the temporary object using the assignment operator. In a well written project, I expect DaysOfWeek and TimeInterval would not have default constructors, because neither object seems to make any sense prior to having contents. If they don't have default constructors then your form of the Course() constructor can't work. That is why it is better to use the form dwhitney67 suggested and I have been repeating. This form avoids the implied call to default constructors that you don't want invoked. |
Quote:
|
Quote:
|
All times are GMT -5. The time now is 11:18 PM. |