   returning from an std::list
  What if you use an STL list and you want to return an element?Like suppose you have this struct:struct Whatever{  string strval;  int ival;};And you have an std::list like this:std::list<Whatever> whatlist;Now suppose you want to set an instance of Whatever equal to a Node in what list?  Let's just say you want it to equal the head Node.int main(){  Whatever copier;  std::list<Whatever> whatlist;  std::list<Whatever>::iterator bullet;  bullet=whatlist.begin();//////what to do here?///////////  copier.strval=bullet->strval;  copier.ival=bullet->ival;  /////////////////////////////////  return 0;}I think the way I did it in the example above makes sense because there's only two members in a Whatever struct.  But what if there were lots of members?  What is a good way to return a Node from an std::list?  Can you overload the "=" operator for the Whatever struct like this:operator =(std::list<Whatever>::iterator);Is that a good way to do it?  Or is there some simpler way?Also what exactly happens when you say struct=struct?  It seems to me that it automatically works even without overloading the "=" operator.  I think it copies all members.  If the struct contained a linked list then I suppose it would fail to make a separate copy of the linked list and only make separate copies of the pointers.  Is my understanding correct?   
  *iter, where iter is an iterator for a container will give you a reference to an element for any of the sequential standard library containers.   ;Quote:Original post by icecubeflowerI think the way I did it in the example above makes sense because there's only two members in a Whatever struct.  But what if there were lots of members? What is a good way to return a Node from an std::list?  Can you overload the "=" operator for the Whatever struct like this:operator =(std::list<Whatever>::iterator);Is that a good way to do it?  Or is there some simpler way?Dereferencing the iterator yields a reference to the element. (That's how the ->'s work: dereferencing yields a reference, and then you select a member of the referred-to element. Remember that x->y in general is the same thing as (*x).y .) You can overload Whatever::operator= to accept a const reference to an element, and then just assign:// "*bullet" does not make a copy of the element; it refers to the existing// element in the list. When you assign, you invoke the operator and copy// the data from the list's element into the local variable.copier = *bullet;// You can also use the element to initialize a variable, by using the// copy constructor:Whatever another_copy(*bullet);// Or even like this:Whatever yet_another_copy = *bullet;// That uses the copy constructor, NOT the assignment operator, because it's// an initialization (i.e. the type is mentioned in the same statement).Quote:Also what exactly happens when you say struct=struct?The operator= for the struct is invoked.Quote:It seems to me that it automatically works even without overloading the "=" operator.That is because, if you don't define an operator= overload, the compiler fills in a default implementation for you automatically.Quote:I think it copies all members.The default implementation recursively attempts to use operator= on each member and base. If it can't do that for some reason (for example, a member might have explicitly declared an operator= overload, but made it private), the compiler will report an error.Quote:If the struct contained a linked list then I suppose it would fail to make a separate copy of the linked list and only make separate copies of the pointers.  Is my understanding correct?If it contained "a linked list" that you made yourself with pointers, then yes; the struct would just contain a root pointer, and just the pointer value would be copied.However, if the struct contained an instance of std::list, then the operator= would be used for the std::list member, and std::list defines operator= explicitly to copy the entire list. Of course, it depends on the element type for the list defining a proper assignment operator and copy constructor. :)   