in an answer to this question I have been informed that my problems lie to the call to a copy constructor in the code below. However, I just don’t see where it’s being invoked. Nowhere am I doing something like agents[1] = agents[0]; although clearly I don’t understand something. Where is this copying occurring and how can I change it so it’s just making new objects each time?
I have been
int main()
{
Level* level;
std::vector<Agent> agents;
level = new Level(agents);
for (int i = 0; i < 1; i++) // this will be more than 1 in the future.
{
agents.push_back(Agent(100, *level, agents, level->Pickups(), D3DXCOLOR(1.0F, 0.4f, 0.4f, 1.0f)));
}
delete level;
}
As others have said,
push_backwill insert a copy into the vector.If you have a compiler with some C++11 support it’s possibly that you can avoid this copy by constructing the object directly into the vector. The new
emplace_backfunction does this:In addition, if the compiler has C++11 support for move semantics,
push_backwill do a move operation instead of a copy operation if a move constructor is available forAgent, because you’re passing it a temporary.