I am currently working on a small game in C++17. To manage elements in the game, I use an ordinary std::vector, which is supposed to manage elements of my cEntity class type.
In the code I currently have it defined like this
std::vector<cEntity> gameEntities;
In the code I also have a function add(), which should add elements of type cEntity.
For example:
void addEntity(void)
{
gameEntities.emplace_back( cEntity{"default values"});
}
Is that performant? I wonder if a cEntity is first created here using the stack and then copied into the vector? Would it be better / more efficient if I didn't manage the specific objects at all, but Smart Pointers instead?
For example:
std::vector<std::unique_ptr<cEntity>> gameEntities;
void addEntity(void)
{
gameEntities.emplace_back(std::make_unique<cEntity>("default values"));
}
Is it faster? The object of type cEntity is created on the heap and only the unique pointer is stored in the vector using move() semantics? I'm not sure about that... With C++ it's always said that you should use the stack wherever possible...
Thank you for a little explanation.