 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
rockenstein Guest
|
Posted: Mon Aug 18, 2003 5:21 pm Post subject: question on std::stack |
|
|
Hi,
I have a question on using std::stack (StlPort):
-------------begin PseudoCode---------
std::stack<myData>;
//push some elements on stack
// get reference to top element
myData elem = stack.top();
// remove top element from stack
stack.pop();
// do something with elem. Is elem still valid after popping?
-------------end PseudoCode---------
According to the documentation top() returns a reference to the top element
of the stack and
pop() removes the first element from the stack. So elem should be invalid
after calling pop?
Or am i missing something here?
greets
Michael
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Kai Ruhnau Guest
|
Posted: Tue Aug 19, 2003 12:18 am Post subject: Re: question on std::stack |
|
|
rockenstein wrote:
| Quote: | I have a question on using std::stack (StlPort):
-------------begin PseudoCode---------
std::stack<myData>;
//push some elements on stack
// get reference to top element
myData elem = stack.top();
// remove top element from stack
stack.pop();
// do something with elem. Is elem still valid after popping?
-------------end PseudoCode---------
According to the documentation top() returns a reference to the top element
of the stack and
pop() removes the first element from the stack. So elem should be invalid
after calling pop?
Or am i missing something here?
|
Your 'elem' is not declared as reference, so stack::top() returns a
reference, which is then copie(constructe)d to elem. So elem remains valid
even if you pop() away the top-Element of your stack.
Greetings
Kai
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Tue Aug 19, 2003 12:22 am Post subject: Re: question on std::stack |
|
|
rockenstein wrote:
| Quote: | -------------begin PseudoCode---------
not really that pseudo ... *shrug* |
| Quote: | std::stack<myData>;
//push some elements on stack
// get reference to top element
myData elem = stack.top();
|
This in fact performs a copy.
| Quote: | // remove top element from stack
stack.pop();
// do something with elem. Is elem still valid after popping?
|
Yes. Note also that doing anything to elem will not alter the topmost
element of the stack.
If you want to keep the reference, use a syntax like this:
myData& elem_ref = stack.top();
For that reference, all your assumptions about invalidation are true though.
cheers
Uli
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Guest
|
Posted: Tue Aug 19, 2003 10:27 am Post subject: Re: question on std::stack |
|
|
| Quote: | I have a question on using std::stack (StlPort):
-------------begin PseudoCode---------
std::stack<myData>;
//push some elements on stack
// get reference to top element
myData elem = stack.top();
// remove top element from stack
stack.pop();
// do something with elem. Is elem still valid after popping?
-------------end PseudoCode---------
According to the documentation top() returns a reference to the top element
of the stack and
pop() removes the first element from the stack. So elem should be invalid
after calling pop?
Or am i missing something here?
|
"elem" is a variable of type myData, not a reference. top () does,
indeed, return a reference, but the compiler copies the item to which
it refers into elem, so elem remains valid after calling pop (). Note
that elem would be *invalid* after pop () if it had been declared as a
reference to a myData:
myData &elem = stack.top();
-- Ron
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|