Birt posted:
1.
class Engine;
class Car
{
Engine& e;
...
};
How to initialize a reference member, e?
First I want you to consider something. Do you want the engine to be A PART
OF the car, or do you just want to associate an engine with a car.
What you're doing is associating an engine with a car. I would pressume that
this is how you'll use it:
class Engine;
class Car
{
Engine& e;
};
int main(void)
{
Engine V12_4litre;
Car RallyCar(V12_4litre);
RallyCar.e.Start();
}
//After main, the destructor for V12_4litre is called,
//followed by the destructor for RallyCar
Here, all that's happening is that my car object, RallyCar, is keeping a
pointer to an engine, (in the form of a reference), but it hasn't actually
assimilated the engine into itself.
You may also consider the following:
class Engine;
class Car
{
Engine& e;
};
Car* pPorscheTurbo;
int main(void)
{
Engine& FastEngine = *(new Engine);
pPorscheTurbo = new Car(FastEngine);
NB Note how I don't delete FastEngine
}
Car::Car(Engine& in_engine) : e(in_engine)
{
}
Car::~Car(void)
{
delete &e;
//This is where FastEngine gets deleted!
}
In the above, the car object is responsible for freeing the memory allocated
for the engine. All the user does is make and engine and pass it onto the
Car.
Another alternative would be to actually assimilate the engine into the car,
ie. there is an actual engine inside the car.
class Engine;
class Car
{
Engine e; //Note how this is an actual engine object!
};
Car::Car(Engine& in_engine)
{
e = in_engine;
}
Here, the car actually makes a copy of the engine and stores it inside
itself. You could use it as so:
Car* pMerc;
void Chocolate(void)
{
Engine& engine = *(new Engine);
pMerc = new Car(engine);
delete &engine;
}
int main(void)
{
Chocolate();
//Although the original Engine object has been deleted,
//the car object has retained a copy of it. Therefore:
pMerc->e.CheckOil();
//*will* work! And the destructor for e is called automatically
//upon destruction of the car!
}
2.
Class Birthdate
{
int month;
int day;
int year;
.
};
class Date
{
int month, day, year;
.
};
Are they the same?
int month, day, year;
is simply an abbreviation of:
int month;
int day;
int year;
No more, no less. So Yes, they are the same.
(What's the story with the fullstop before the closing brace?)
3.
class Date
{
int month;
int day;
int year;
public:
Date(int m, int d, int y);
.
};
class PersonInfo
{
Date birthday;
.
};
PersonInfo:: PersonInfo(int m, int d, int y) : birthday(m, d, y)
{
...
}
"birthday" in class PersonInfo looks like calling the default constructor
that have been replaced by Date(int m, int d, int y);
Could someone explain how this still works?
First of all, you don't have a default constructor for "Date". You could
consider the following:
Date:
ate(int m = 1, int d = 1, int y = 1950) {};
Once you supply any constructor at all, the default one disappears.
Read the following sentence slowly: You are correct that the object
"birthday" of the class "Date", which is an object of an object of the class
"PersonInfo", that it's constructor is called with the parameters passed to
the constructor of PersonInfo. You have achieved this perfectly.
That is, the constructor for birthday is supplied with the data given
to the constructor of a PersonInfo object.
Hope all that helps.
-JKop