Cast multiple vectors to void *

158 Views Asked by At

I am currently working on a Roguelike where I have multiple different event queues(in the form of vectors), that handle multiple different event categories. Or rather, different game objects have their own private event queues.

Within the map tile data, I have a special trigger structure that activates when the player (or anything else) enters the tile. This tile structure provides data for generating an event flag, which then needs to placed onto the appropriate event queue.

The tile structure is as follows:

struct tile{
  int t_type;
  trigger t_dgram;

  vector<actor*> occupied;
  vector<item*> loot;
public:
  void trigger_tile(int ns, int we, );
};

The trigger structure itself can hold information for determining which queue is the appropriate one, but it's infeasible to hold pointers to all possible event queues for every tile in the map, particularly as I'm having maps dynamically generate terrain on the fly for infinite maps.

So the question is twofold: Would it be a good idea to cast a vector to a void pointer when calling the function:

void tile :: trigger_tile(int ns, int we, void *queue_ob);

The tile object itself will be able to determine the correct queue vector type and dereference the void pointer using the trigger t_dgram member variable, but is this a good idea? Is there some danger to this that I'm not aware of?

How would you personally suggest I achieve a similar effect?

Thank you for your time.

2

There are 2 best solutions below

0
On

So the question is twofold: Would it be a good idea to cast a vector to a void pointer when calling the function:

No, that does not sound right. Perhaps you meant to cast a pointer the vector to a void*, which will be OK as long as the downstream function that uses the void* casts is back to the vector pointer before accessing the elements of the vector.

0
On

The "trigger" sounds like a class to me.

struct Trigger {
    virtual void triggeredOn(tile *tile) = 0;
}

Then tile has a pointer to its trigger:

struct tile{
  ...
  ...
  Trigger *trigger;
  void trigger_tile(int ns, int we, ){trigger.triggeredOn(this)};
};

Now classes derived from Trigger can hold pointers to any queues or even do something that does not involve queues.