vector and objects (c++)

Started by
13 comments, last by Todo 16 years, 3 months ago
You're calling a non-const function using a const_iterator. Make DisplayStats const, or use a non-const iterator.
Advertisement
Quote:Original post by Driv3MeFar
Quote:Original post by Sneftel
Actually, you were right making that statement, and yours is the non-conformant compiler. The standard specifies that non-const references cannot be bound to temporaries. More here.


And here.


Good to know that non-conformant compiler seems to be Microsoft's very own that came with VS2008 then ;-). Anyhow, thanks for clearing that up (both of you).
Quote:Original post by raffagapro
Quote:Original post by Todo
Quote:Original post by raffagapro
1. Is IPool just saving references of Item Object into its vector m_Items or is it actually making a copy of the object?


A copy.



So if I wanted to store 100 objects in the vector inside IPool, I would ende up using 200 chunks of memory; 100 when defining the Item objects and 100 when calling the IPool::Add functing?


Yes, but the first 100 would then be destructed almost immediately, and the memory made available again.

Actually, there might be more chunks of memory being allocated freed, depending on the initial size of the vector and how you add the elements. But at the end of the process, you won't be wasting memory: temporary copies get cleaned up automatically.

EDIT: Also, it looks like you really need to read the section on const correctness, too.
Thanks that article helped me understand a bit more about "const correctness". I feel like I almost got it. I might be running to a similar problem here:

void bagStore(string item, vector<string>& rModBag);int main(){const int MAX_BAG_SLOTS = 11; //needed to work on setting a set # of slotsvector<string> heroBag(MAX_BAG_SLOTS, "Empty");bagStore("Helm of Wrath", heroBag);}void bagStore(string item, vector<string>& rModBag){	vector<string>::iterator bagIter;//iterator to acess the vector	string empty = "Empty";	bagIter = find(rModBag.begin(), rModBag.end(),empty);//find first empty slot and stores item	//cout << *bagIter;<---use to see the changes while running the program	*bagIter = item;//changes the object in the vector for the string item	//cout << *bagIter;<---use to see the changes while running the program	//system("pause");<---use to see the changes while running the program}


I don't get a compile error, but the changes to heroBag alias "pModBag" in bagStore function aren't being pass back to the vector heroBag, any ideas why?
It should work. At least on my end it works fine. On exit the item "Helm of Wrath" is added to the vector heroBag.

This topic is closed to new replies.

Advertisement