Is there a better way to get an object with an ID as parameter?

I have a Tile class which contains various static Tile objects, each with their own unique ID. Some of the tiles need to be updated differently than the others, so I have subclasses override the virtual function updateTile(). I need to call this function with the object that corresponds to the tile's ID.

Right now I'm using a std::vector of Tile*'s and a "get" method to return the Tile* at the ID passed to the method. I was wondering if there was an easier way to do this and whether this is a memory leak, since I'm not very experienced with pointers. Thanks in advance.
Last edited on
If no two objects can have the same id, you could use a set sorted by it (search would be faster, but you'd loose random access). Also, you may want to throw away ids at all and just use addresses instead (search wouldn't be needed any more. I don't see any downside to this).
By address do you mean the location of the object in the vector? Because that's what I'm doing now. I probably should have been more specific.
Oh. In that case, ignore the set suggestion... I assumed ID is a member of Tile class. If you're referring to position in the vector, index is a more common term.
Now, about the address, if you have vector<Tile*> my_vector;, every element in it is a pointer to a tile. If you saved that pointer to some Tile* ptr = my_vector[some_id]; you could do ptr->updateTile(); instead of my_vector[some_id]->updateTile();. If you pass a pointer to the function, it will not need to know about my_vector or some_id.

hamsterman

Also, you may want to throw away ids at all and just use addresses instead (search wouldn't be needed any more. I don't see any downside to this).


can you give some example (code snippet ) .. its hard to understand .
If I were to do that I would need a grid of pointers instead of the grid of indexes I have now. A grid of indexes would be better, right?

EDIT: @bluecoder: He means it would be easier if I didn't use, say, std::strings, and instead use the address of the object in the std::vector.
Last edited on
ok ..got it .
thanks hamsterman and Pyrius ..
If I were to do that I would need a grid of pointers instead of the grid of indexes I have now. A grid of indexes would be better, right?
You mean the map? I don't see how one would be better than the other. They do the same thing. Using addresses removes one layer of indirection though. Also, should you ever want to erase something from your vector, if you used ids, they would change, while addresses would stay the same.

@bluecoder
I assume that he now has
1
2
3
4
5
6
7
8
int main() {
   vector<Tile*> tiles;
   //...
   for(int i = 0; i < tiles.size(); i++) update(tiles, i);
}
void update(vector<Tile*> tiles, int id) {
   tiles[id]->updateTile();
}

I am suggesting
1
2
3
4
5
6
7
8
int main() {
   vector<Tile*> tiles;
   //...
   for(int i = 0; i < tiles.size(); i++) update(tiles[i]);
}
void update(Tile* ptr) {
   ptr->updateTile();
}

I assume that he now has
1
2
3
4
5
6
7
8
int main() {
   vector<Tile*> tiles;
   //...
   for(int i = 0; i < tiles.size(); i++) update(tiles, i);
}
void update(vector<Tile*> tiles, int id) {
   tiles[id]->updateTile();
}



I have something like the following:

1
2
3
4
5
6
7
8
9
10

void Level::updateLevel()
{
   for(int i = 0; i < tiles.size(); i++) tiles[id]->updateTile();
}

void Tile::updateTile()
{
   // Code
}


If it makes any difference.
Last edited on
Okay, then I guess my suggestion only makes difference when map is involved.. Could you elaborate then what the original problem is?
I have a Tile class which contains various static Tile objects
¿why does a Tile need to know about its brothers?

I don't see how are you using the id in that code.
There wasn't a problem, I was just wondering if could do the same thing in an easier way, or maybe do the same thing without pointers, and whether it causes a memory leak. I'm new to pointers, and I assume it doesn't, but I don't want a memory leak with something I call so many times per frame.

EDIT: @ne555: The Tile objects are static, which means they aren't members of other objects of the class. You access them with the syntax "Foo::Bar". If not in the Tile class, I don't know where else I'd put the objects.

By ID I meant the address of certain attributes of the tile which are stored in vectors. To access the attributes, I would type "Foo.at(tileID)".
Last edited on
thanks hamsterman for the explanation ...thank you very much
Calling updateTile() doesn't cause any leak, unless there are some news and no deletes in it.
Alright, thanks hamsterman
Topic archived. No new replies allowed.