G
gamehack
Hi all,
Let me first start off with this piece of code:
for(int i = 0; i < NumBoxes; ++i)
{
Box b(i, start, money[15], false);
this->boxes.push_back(b);
start++;
}
Here a Box object is created on the stack as soon as the for loop is
entered. My question: Is it true that a copy of the object is pushed
onto the vector? And after the loop ends, the object b is destroyed.
What I'm concerned about is that if NumBoxes is big, there would be a
lot of overhead just creating and destroying the same object. Is it
possible to allocate one object dynamically and just reuse it? I can do
something like:
Box* b = new Box(i, start, money[15], false);
But then what would be my argument for push_back? Would it be
this->boxes.push_back(*b); ?
Thanks a lot
Let me first start off with this piece of code:
for(int i = 0; i < NumBoxes; ++i)
{
Box b(i, start, money[15], false);
this->boxes.push_back(b);
start++;
}
Here a Box object is created on the stack as soon as the for loop is
entered. My question: Is it true that a copy of the object is pushed
onto the vector? And after the loop ends, the object b is destroyed.
What I'm concerned about is that if NumBoxes is big, there would be a
lot of overhead just creating and destroying the same object. Is it
possible to allocate one object dynamically and just reuse it? I can do
something like:
Box* b = new Box(i, start, money[15], false);
But then what would be my argument for push_back? Would it be
this->boxes.push_back(*b); ?
Thanks a lot