Board index » cppbuilder » STL stack.pop() question

STL stack.pop() question


2004-06-29 11:09:01 PM
cppbuilder110
sorry if this is not the appropiate place to post this..but its short
anyway..
hi there all you all. I in a dilema here...
stack<employee>my_stack;
my_stack.push(employee("john1"));
my_stack.push(employee("john2"));
employee i = my_stack.top();
my_stack.pop();
my_stack.pop();
My question is how 'i' is assigned to the top of the stack since the 'top'
method return a reference. what is the mechanism here? copy constructor?
thanks in advance.
--
[Tío konZa]
 
 

Re:STL stack.pop() question

KonZa < XXXX@XXXXX.COM >writes:
Quote
sorry if this is not the appropiate place to post this..but its short
anyway..
If you are using Bolrland C++, then you are right here. But please have
a look at info.borland.com/newsgroups/ and read the newsgroup
descriptions and guidelines before your next post, even if it's a short
one again. This will help you find the appropriate newsgroup.
Quote
stack<employee>my_stack;

my_stack.push(employee("john1"));
my_stack.push(employee("john2"));

employee i = my_stack.top();
my_stack.pop();
my_stack.pop();

My question is how 'i' is assigned to the top of the stack since the
'top' method return a reference. what is the mechanism here? copy
constructor?
i is never assigned in this snippet. It's just initialized on the line of its
definition; it will be destructed once it goes out of scope.
The initialization of i takes a reference to employee as its argument.
This means that the employee copy constructor will be used to initialize i.
 

Re:STL stack.pop() question

On Tue, 29 Jun 2004 18:39:21 +0200, Thomas Maeder [TeamB] < XXXX@XXXXX.COM >
wrote:
Quote
The initialization of i takes a reference to employee as its argument.
This means that the employee copy constructor will be used to initialize
i.
so if the employee class doesn't has a copy constructor neither &=
operator overloaded
bad things could happend.
so this is solved either using a copy constructor or the &= oeprator
overloading right?
thanks in advance
--
[Tío konZa]
 

{smallsort}

Re:STL stack.pop() question

KonZa < XXXX@XXXXX.COM >writes:
Quote
>The initialization of i takes a reference to employee as its argument.
>This means that the employee copy constructor will be used to
>initialize i.

so if the employee class doesn't has a copy constructor neither &=
operator overloaded bad things could happend.
First of all, your question doesn't have anythign to do with operator&=();
That operator is used for bitwise "ANDing" in a value into another value.
Do you mean the copy-assignment operator (operator=())? It isn't invoked here,
either, but its function is somewhat similar to that of the copy constructor.
Second, the employee class is very likely to have a copy constructor. If
you don't "manually" declare the copy constructor in the class definition,
the compiler will generate one that will perform a member-wise copy of
employee objects. In most cases, this is exactly what is needed.
Bad things will only happen if the copy constructor (user-defined or
compiler-generated) doens't do what it should do. I can't tell from here
if this is the case with your employee class.
Quote
so this is solved either using a copy constructor or the &= oeprator
overloading right?
Again, operator&=() is not involved.
*Using* a copy constructor does not solve anything. The copy constructor
is used, and you can't do anything about it. Depending on the intended
behavior of employee objects, you might have to provide the copy constructor
yourself.
If you provide the copy constructor yourself, you should consider if you
should also provide the copy-assignment operator. Often, the destructor of
such class has to be user-defined as well.