Let's say I have two classes, a Car and the player's HeadsUpDisplay.
The HeadsUpDisplay display certain things it gets from Car, like speed_ and fuel_.
The HeadsUpDisplay should also be able to "switch" Cars.
Thinking about this I reached the conclusion that I could have a const Car *car_; inside the HeadsUpDisplay class, and when everything is done I'd just set car_ = NULL;.
There would also be a HeadsUpDisplay::set_car(const Car &car) {car_ = &car;}.
My questions are, how would you do this yourself, and is my method flawed?
If the HUD is the same for all the cars (graphically), then you should only refer to one instance of the HUD class/struct. If they're different, then you could do a few things:
1) Create a vector (or something similar) that contains the HUD for each vehicle. Each HUD has an attached identifier. The identifier is used to link the HUD to the vehicle that has the same identifier (be careful! don't give two or more HUDs the same identifier).
2) Make the HUD part of the base vehicle class (like what Firedraco said).
[...] should the HUD be a friend class of the Car [...] ?
If you make HUD a nested class you don't have to make it a friend.
A nested class is a member of the enclosing class and, as such, it
can freely access the enclosing class's other (private) members.
Thanks r0shi, I didn't know about nested classes.
But can nested classes contain virtual members to be overridden?
I'm also thinking that the Car class could become the superclass for say a Ferrari class which indeed can have its own HUD, but that doesn't feel right.