A game class design question

Hello everybody. This is a hypothetical question.

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?

Thank you for reading.
Sounds fine to me. I'd probably make the HUD class part of the car though (but that's just me).
closed account (zb0S216C)
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).

Wazzak
Making the HUD part of the Car makes a lot of sense for different HUDs.

If I go that way, should the HUD be a friend class of the Car, and then pass the Car's this to the HUD constructor?

I assumed that by "being a part of" you meant being an instantiated object.
Catfish wrote:
[...] 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

class Outer
{
    class Inner
    {
        Outer * owner;

    public:

        Inner(Outer * owner_): owner(owner_) {}

        void DoIt()
        {
            std::cout
                << owner->data
                << std::endl;
        }
    };

    int data;
    Inner inner;

public:

    Outer(int n): data(n), inner(this) {}

    void DoIt() { inner.DoIt(); }
};

int main()
{
    Outer outer(10);

    outer.DoIt();
}

You might also want to google 'observer pattern'.
Last edited on
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.
Catfish wrote:
But can nested classes contain virtual members to be overridden?

Sure.

Catfish wrote:
I'm also thinking that the Car class could become the superclass
for say a Ferrari class which indeed can have its own HUD

I guess you could do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>

class Car
{
protected:

    class HUD
    {
        Car * owner;

    public:

        HUD(Car * owner_): owner(owner_) {}

        virtual ~HUD() {}
    };

    HUD * hud;

public:

    Car() {}

    virtual ~Car() {}
};

class Ferrari : public Car
{
    class FerrariHUD : public Car::HUD
    {

    public:

        FerrariHUD(Car * owner_): Car::HUD(owner_) {}

        ~FerrariHUD()
        {
            std::cout
                << "~FerrariHUD"
                << std::endl;
        }
    };

public:

    Ferrari() { hud = new FerrariHUD(this); }

    ~Ferrari() { delete hud; }
};

class Lamborghini : public Car
{
    class LamborghiniHUD : public Car::HUD
    {

    public:

        LamborghiniHUD(Car * owner_): Car::HUD(owner_) {}

        ~LamborghiniHUD()
        {
            std::cout
                << "~LamborghiniHUD"
                << std::endl;
        }
    };

public:

    Lamborghini() { hud = new LamborghiniHUD(this); }

    ~Lamborghini() { delete hud; }
};

int main()
{
    Car * car1 = new Ferrari;
    Car * car2 = new Lamborghini;

    delete car1;
    delete car2;

    return 0;
}

I'm not sure if this is the best way to do it though...
Topic archived. No new replies allowed.