Discussion:
[Dev-C++] STL stack/vector pushing class objects
Matt Kerr
2003-10-25 09:25:03 UTC
Permalink
This may seem trivial, but I'm having troubling finding a clear answer
through just searching on the net.

My question is:
In the following scenario where you are using STL stack or vector
container with a class object, ie:

stack<board> moves;

if you do something like:

moves.push( thismove );

Does thismove get copied or is it passed into the stack by reference?

In other words, if I am dynamically allocating thismove, should I be
worrying about deallocating (freeing) it at a later point? Or does pop()
call the deconstructor on the object for you?

Thank you!
Michal Molhanec
2003-10-25 16:22:10 UTC
Permalink
Post by Matt Kerr
stack<board> moves;
moves.push( thismove );
Does thismove get copied or is it passed into the stack by reference?
copied
Post by Matt Kerr
In other words, if I am dynamically allocating thismove, should I be
worrying about deallocating (freeing) it at a later point?
yes, you should
take a look at smart pointers for solution
http://boost.org/libs/smart_ptr/smart_ptr.htm

Loading...