Jump to content
  • Advertisement
Sign in to follow this  
STRATERCAS

STL::vectors

This topic is 4827 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Yes. A vector copies the object into itself, so usually you get a destructor call when the copy on the stack goes out of scope.

Share this post


Link to post
Share on other sites
Objects yes, but be careful with ponters. If you have a vector of pointers you must explicitly call delete on the pointer for the deconstructor to be called (or else you'll end up with a memory leak).

If you need to use a vector of pointers you might want to consider looking into boost::shared_ptr.

Share this post


Link to post
Share on other sites
Only very samll objects with simple copy semantics should be used in vectors. It is much better to use pointers as elements. The container copies it's elements frequently. See the well-known Essential STL by Meyers for a lengthy discussion of this......


Share this post


Link to post
Share on other sites
Quote:
Original post by AzCoder
It is much better to use pointers as elements.


I wouldn't recommend using STL containers with pointers to a beginner - until they are comfortable with pointers and memory management - or need polymorphism. There are just too many gotchas associated with using pointers and containers.

Quote:
See the well-known Essential STL by Meyers for a lengthy discussion of this......


Good read - though the first time I read that tip I misunderstood what he was getting at.
However, he also points out the negatives associated with pointers and containers like vectors. IIRC he also suggests getting familiar with boost::shared_ptr

Share this post


Link to post
Share on other sites
Ok it started doing it again.
Here is what I stripped it down too.
I am only by passing by value now(I was using pointers before). This isn't how I wanted to do this but, when it get's to the cout there should be two Item in existance should there not(One in int main() and one in the vector)? Now my output is -1 and not 1 at the cout and -4 when the program ends. The constructor is getting called only once but the deconstructor is getting called 5 times, that doesn't make sense to me.


class ITEM
{
ITEM(){ s_m_ITEMCOUNT++; char buffer[10]; itoa(s_m_ITEMCOUNT, buffer, 10); MessageBox(NULL, buffer, "Items", MB_OK);}
~ITEM(){ s_m_ITEMCOUNT--; char buffer[10]; itoa(s_m_ITEMCOUNT, buffer, 10); MessageBox(NULL, buffer, "Items", MB_OK);}

...
}



void LAWNSQUARE::m_Add_Item(ITEM new_Item)
{

v_my_Items.push_back(new_Item);

}

int main()
{
....
ITEM someThing;
myLawnSquare.m_Add_Item(someThing);
cout << "There are " << ITEM::s_m_ITEMCOUNT << " total Items in existance!" << endl;

....
}


Quote:
See the well-known Essential STL by Meyers for a lengthy discussion of this......


I'll have to pick that one up.

[Edited by - STRATERCAS on August 27, 2005 10:25:32 PM]

Share this post


Link to post
Share on other sites
There's another way your ITEM object can be created, by using the copy constructor. This is usually the way a vector works, it'll call the copy constructor on your object-to-insert and adds the copy to its list.

Remember, once you do something like ref counting, you need to implement at least three cases in your class, better 4 though:

Default Constructor (parameterless constructor)
Copy Constructor
Destructor
and optional, assignment operator


The copy constructor has this signature:

ITEM::ITEM( const ITEM& rhs )
{
// copy data, increase ref count
}


Some special cases to keep in mind: In the copy constructor and assignment operator you may want to check if you get yourself as parameter! (Check with &rhs == this)

Share this post


Link to post
Share on other sites
OHH, copy constructors. That makes perfect sense. That would explain why it was decrementing and not incrementing. I can't believe I forgot about that :D. Thanks a million!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!