populating a vector of objects

This code seems to work, but there is A LOT of destructors being called. The same items destructor seems to be called repeatedly. As the loop progress, with each loop the destructor each of the previous objects is called. Is this normal? Is there a better way to load a vector? The concept is a Store or Hero's Inventory system so it has to be flexible.

1
2
3
4
5
6
	for(int i = 0; i < 3; i++) 
	{	
		cout << "Creating item " << i << endl;
		myVector.push_back(items(enum)); 
	}
	cout << "playerClass " << name << " created" << endl;



Player's items generator created
Creating item 0
DULL LONGSWORD items object destroyed
Creating item 1
DULL LONGSWORD items object destroyed
RUSTY DAGGER items object destroyed
Creating item 2
DULL LONGSWORD items object destroyed
RUSTY DAGGER items object destroyed
BROKEN HAMMER items object destroyed
Player created


Thanks in advance all!
And use reserve() to ensure your vector have capacity to hold all values without reallocation:
Lines 5, 8, 9 in your output are probably there because of underlying array reallocation.
Last edited on
Thanks for the response. I'm still using 2010. This would be invisible to me except i have couts in my destructors for debugging and i'm just curious if this is normal behavior or bad code, and if it's bad code what are some better ways to do it?

Now that i read the description for push_back again, is this just happening because the object is destroyed everytime it's pushed back down the vector? I'm new to both vectors and classes and trying to understand the underlying mechanics.

The difference between emplace_back and push_back seems to be that emplace constructs in-place and push_back constructs and copies?

Thanks again
When you use push_back like in your code, you construct a temporary, THEN pass it to push_back, which makes a copy of the object to put in the vector, and then push_back ends and returns to your code where the temporary is not needed anymore, so it is destroyed. The construct-copy-destruct is very slow.

When you use emplace_back like I suggested, you construct a temporary, THEN pass it to emplace back, which moves the contents of the temporary into the vector, and then emplace_back ends and returns to your code where the temporary is not needed anymore, so it is destroyed. The construct-move-destroy is much more efficient.

The output you are seeing is perfectly normal, though because of the copies it is indeed inefficient.

I recommend you look up move semantics.

Bugsplatter wrote:
I'm still using 2010
THis could refer to anything, but I am assuming you mean Microsoft's Visual C++ 2010 IDE? If so, I feel very very sorry for you.
Last edited on
I'm too ignorant to know what i'm missing by using MVC++ 2010 I guess!

On the question at hand, i used reserve() as MiiNiPaa suggested and that cleared out all the extra destructor calls. I'm sure i'll have to upgrade to MVC++ 2012 pretty soon so I'll switch to emplace_back at that point, but this all cleared this up well. Thanks.
closed account (3qX21hU5)
You can still use C++11 with MVC++ 2010 as long as the compiler you use supports C++11. Meaning if you like MVC++ 2010 you can just go download a up to date compiler that supports C++11 and use it with your current version of Visual Studios.
Last edited on
Topic archived. No new replies allowed.