简体   繁体   中英

Making a list of variables without copying them

The Main question:
How do you create a pointer to a variable that can be stored and then used to access this variable at a later time Without a copy being created


The below is here just to answer any questions that people might have as to why I want to do this

Prologue:
I am making a game with DirectX and I want to create a "list" of Entities(A special class) in a another class. I want to do this so I can keep track of all the objects in the game that are rendered by a specific method(One list for triangles, another for lines, ect). To do this I originally just had a class and it had a std::vector<Entity> , the class then had an add(Entity entity) function which would add the specified entity to the vector. This worked out very well until I started trying to make changes to these entities in the vector.

The problem:
First I would create an entity in the main world loop, Entity testEntity = Entity(position); then I would add it to the entity list, entityList.add(testEntity); . When this command is called it is actually just making a copy of the entity as it is at the time that the add command was called. This means that there are suddenly 2 entities that represent 1, The entity in the main world that is being affected by all the game logic, and the entity in the entityList that does not update, but renders. These two are not in sync.

The desired effect:
The entityList's std::vector is actually just filled with some sort of pointer to the entities in the world loop. Then when an entity is updated in the world loop the entityList has the same data for that entity.

It's not entirely clear to me where you're having trouble, so this may not answer the question:

It seems like you want to just store a vector of pointers to Entity objects. Ie std::vector<Entity*> .

If you know that testEntity will be in scope for the lifetime of the vector, you could just add a pointer to it to your vector. Ie entityList.add(&testEntity) .

If that assumption isn't true, you probably want to allocate your Entity objects on the heap (eg Entity* testEntityPtr = new Entity(position); . If you're using C++11 (or maybe even if you're not), you probably want to use shared_ptr and make_shared in this situation.

You could possibly use the c++ 11 Move Semantics to preserve your data. If Entity have pointer members that point to some allocated data that you do not want to copy you could implement Move semantics that would essentially transfer ownership the the copy that you are placing in the vector. For example:

Entity(Entity&& entity)//move constructor
{
    this->data = std::move(entity.data);
    //and so on.
}

You will also need a "Move assignment operator" Entity& operator=(Entity&& entity);

You will need to look up "Move Semantics" and "rvalue references" for more info.

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM