Assignment operator and iterators

This is a discussion on Assignment operator and iterators within the C++ Programming forums, part of the General Programming Boards category; Hi all! Sorry for so many questions. If you feel that I'm abusing this board, please say so, and I'll ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    29

    Assignment operator and iterators

    Hi all!

    Sorry for so many questions. If you feel that I'm abusing this board, please say so, and I'll go elsewhere... I typically consult google, cplusplus.com, codeguru, my books, search the forum here, and then fiddle with code before posting. I'm just a bit slow going with learning some of these features :P

    In any event, is it possible to return a user-defined object from a list?

    This is my dilemma:

    I have a list of custom objects. I need to be able to copy one object from the list into another object not in a list.

    Ex (assuming the assignment operator for myobject is overloaded):

    Code:
    list<myobject> mylist;
    list<myobject>::iterator myiter;
    myiter = mylist.begin();
    
    myobject standAloneObject;
    
    standAloneObject = myiter; //?? Needs to copy the object where myiter points to into standAloneObject
    Unfortunately, this doesn't work because the compiler states that it can't copy from an iterator object to a myobject type.... I've tried using *myiter to get the object, but evidently that is illegal as well. So, is there any way to get at the object that's stored in the list?

    Thanks again for all of your help!

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    Code:
    list<myobject> mylist;
    list<myobject>::iterator myiter;
    myiter = mylist.begin();
    
    myobject standAloneObject;
    
    standAloneObject = *myiter;
    This should work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    Hmmm....

    Seems to work, which is weird, because I tried that earlier. Maybe I'm just having a case of the Fridays :P

    Thanks again! If I can ever be of assistance to anyone on this board, please don't hesitate to contact me. You guys have been so wonderful to me!

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    An iterator is supposed to look and feel similar to a pointer. Just as you dereference pointers, you also "dereference" iterators.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21