开发者

Pointer that can point to multiple classes

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 开发者_JS百科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 Player and NPC. This base class can have common field from Player and NPC or just can be empty (but should have virtual destructor), then you can store pointer of that type.

class Target
{
public:
    virtual ~Target(){}
};

class Player : public Target
{
    ...
};

class NPC : public Target
{
    ...
};


You could make a base class Targetable, from which both Player and NPC inherit, then use a pointer of type Targetable*.


You can create a base class if they really have some common interface. Maybe something like this:

class Target { /* common interface, like a method both Player and NPC use */ };
class Player : public Target { ... };
class NPC    : public Target { ... };

If you don't have a common interface you shouldn't use this. You then have to rethink your design.


You should use inheritance by deriving your classes Player and NPC from a base class, say GameCharacter (which on its hand is possibly derived from GameObject or what ever). Then you could just declare one target: GameCharacter* target;


  1. Use inheritance, derive both classes from a single base class as others have suggested.
  2. Declare a union having pointers of both of these classes.
  3. Use void*
  4. Use C-style or reinterpret_cast to convert one class to another class type.


Ah ah !

I'm programming a game too, and I did a bit of conception myself.

Using inheritance is the good way, but the answers with a "targetable" base class are not optimal. Obviously, what "character" and "non player character" have in common (inheritance) is not the "targetable" attribute, it's the whole "character" : (tree is for inheritance)

  • character :
    • non-player character
    • player character

If you think about it, the "targetable" property is not restricted to characters. That gives us :

  • interactive things : (targetable but also have a size, a weight, etc.)
    • passive or inanimated = object, artifact (you may want to target an object)
      • door (for example bashing a door)
      • statue
      • etc.
    • active or animated = usually living things
      • mobs (can fight but not speak with a player)
      • characters (additional functions like speaking)
        • non-player characters
        • player characters

Of course this is a very rough conception, but you get the point. What I mean is that you'd better do paper-design if you don't want to re-implement it again and again.

Good luck !

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜