Jump to content
  • Advertisement
Sign in to follow this  

Storing Units

This topic is 3431 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a vector which is filled with Units. std::vector<Unit> Units; I need a way to store the location(The int put between []) of a Unit in the vector. The problem is that erase(); will be called. So the location of a Unit in the vector can change. What is the best way to store the Units? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Could you please be a little more specific?

What do you intend to do with the index value of each unit? As you said, index values can change. Can't you use a pointer to a unit instead? What do you mean by "store the location [index] of a unit in the vector"?

Share this post


Link to post
Share on other sites
For Example:
Unit[1] <- The Unit I need.
But Unit.erase( 0 )

This makes The new location of the unit 0. So now:
Unit[0] <- The unit I need.

The problem I had with pointers is that when I push_back to much. The value's that the pointer points to are lost.

When I reserve(); . I can use pointer. But the problem with that is, when I get above reserve the pointers have to be reset again.

Share this post


Link to post
Share on other sites
If you're up to it, you could always use a vector of pointers to Units:
std::vector<Unit*> Units;
You would really have to know what you are doing though, but this way you can simply copy the pointer and use it as a reference to the Unit from another place in your code.
Just make sure you delete whatever you create with new, but when using one vector as your primary storing point, you can simply delete before erasing. And be mindful of the dangling copies of pointers you might create.

Share this post


Link to post
Share on other sites
Quote:
Original post by M4rtin
For Example:
Unit[1] <- The Unit I need.
But Unit.erase( 0 )

This makes The new location of the unit 0. So now:
Unit[0] <- The unit I need.

The problem I had with pointers is that when I push_back to much. The value's that the pointer points to are lost.

When I reserve(); . I can use pointer. But the problem with that is, when I get above reserve the pointers have to be reset again.
I take it you want other objects to be able to store references ('conceptual' references - not necessarily C++ references) to the units in the array so that they can interact with the unit in some way? Can you provide any more details on what you're trying to do?

Any sort of index, iterator, or pointer that stores the address or location of a unit in the array is subject to invalidation when the array is modified. One option would be to use e.g. std::list, which has different behavior than std::vector with respect to iterator/pointer invalidation. Another option would be to store pointers in the array, allocate the Unit objects dynamically, and then pass out pointers to other objects who need them.

In either scenario, you'll have to deal with the issue of ownership and dangling references. A common solution in C++ is to use a 'smart pointer' to manage the lifetime of the object; this makes it safe (or at least safer) to store multiple references to the same object. An extension of this concept is the 'weak reference' (e.g. boost::weak_ptr) which offers different ownership semantics than your typical 'shared' pointer, and can be preferable in certain circumstances.

Again though, some more details about the actual problem would help.

Share this post


Link to post
Share on other sites
The suggestions seem like what I needed.

Just to be sure: I want a pointer to an object in a vector class. But the vector class can resize. So I "loose" the pointer. While the value still is in the vector.

Share this post


Link to post
Share on other sites
Although I do not know what solution you decided to take, I do not really see what you mean by "loosing" the pointer.
Vectors can resize by (sometimes) reallocating their data. This means the location in memory may change if you insert or delete elements. That is why a pointer to an element stored directly in a vector is not safe.

Share this post


Link to post
Share on other sites
Yes, that is what I mean. The pointer doesn't point to the right data anymore.

So a smart pointer doesn't have that problem?

Share this post


Link to post
Share on other sites
The problem is solved by storing pointers to the Units (either smart pointers or plain ones) in the vector, instead of the Units themselves. In this case, the pointers (not the Units!) are reallocated, so their values/addresses are retained. Then you can use the pointers stored in the vector as a reference to your Units.
Again, if you are not familiar with pointers, this can be very dangerous.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!