I’m developing a game. There’s a “Player” class and an “NPC” class.
Each instance of these classes can have a “target”. That target can be another player or another NPC. I’d like this “target” value to point to the instance of the Player/NPC that the Player/NPC has selected.
So the problem being, with my current setup I’ve got 2 pointers for each Player/NPC in the form of:
Player* target_p;
NPC* target_n;
This obviously isn’t optimal. What I’m asking is if there is a way to make a pointer which can point to either of these classes, and not just one.
Thanks in advance.
You can make common base class for
PlayerandNPC. This base class can have common field fromPlayerandNPCor just can be empty (but should have virtual destructor), then you can store pointer of that type.