C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

question on std::stack

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
rockenstein
Guest





PostPosted: Mon Aug 18, 2003 5:21 pm    Post subject: question on std::stack Reply with quote



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





PostPosted: Tue Aug 19, 2003 12:18 am    Post subject: Re: question on std::stack Reply with quote



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





PostPosted: Tue Aug 19, 2003 12:22 am    Post subject: Re: question on std::stack Reply with quote



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





PostPosted: Tue Aug 19, 2003 10:27 am    Post subject: Re: question on std::stack Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.