Passing reference from a iterator

This is a discussion on Passing reference from a iterator within the C++ Programming forums, part of the General Programming Boards category; I have a problem to solve. I am working my way tgrough a list with an iterator. At some points ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    1

    Passing reference from a iterator

    I have a problem to solve. I am working my way tgrough a list with an iterator. At some points I need to pass the address of the object that the iterator is pointing to. How do I do this? My code look somewhat like this:

    Code:
     list<TASK>::iterator taskIter;
    ...
     for (taskIter = task.begin(); taskIter != task.end(); ++taskIter)
          {        
            if (taskIter->getNextXmit() <= time)
              {
                 newMsg = new MSG(address of the particular task);  <- here is my problem
               }
           }
    -----
    class MSG
    {
      
    protected: 
      TASK  *taskPtr;                      /* parent task                         */
      ...
          
    public:
    ...
    MSG::MSG(TASK *task)
      {
         this->taskPtr = task;
      }
    ...
    In my MSG object I need a pinter to the TASK object, so I am trying to pass it. I thought that the iterator was, basically, a pointer to the object, but I can not find out how to get it to look like a pointer to the object.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    The problem is that it's undefined behavior to take the address an iterator holds, if any (as I'm believed to understand).
    It's better that you design an interface to keep the iterator instead of forcing the address out of it.
    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
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Quote Originally Posted by Elysia View Post
    The problem is that it's undefined behavior to take the address an iterator holds, if any (as I'm believed to understand).
    Not at all, if the iterator models at least ForwardIterator. (std::list's iterators do.) &*it is a valid way of getting the address.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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