CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Threaded View

  1. #1
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238

    Resolved How to convert from a const iterator to a non-const iterator? [Resolved]

    Ok, here's the below snippet that's causing the problem.
    Code:
    bool graph::find_node(const std::string & node_name, const std::list<node * > & list)
    {
     for(std::list<graph::node * >::iterator iter = list.begin(); iter != list.end(); iter++)
     {
      if(( * ( * iter)).name == node_name)
      {
       return true;
      }
     }
    
     return false;
    }
    Now the list that I'm using is a constant and I would like it to remain like that in order to prevent it from being changed in any way. However, it seems that the iterator that is created from it (in the boldened line) is also a const. How could I create a non-const iterator in this instance? Or convert the const iterator to a non-const one after creation?

    [edit]

    I could change the passed in list to a non-const and it'll work, but I would not like to break good programming practices in the process, which is the reason for the question.
    Last edited by YourSurrogateGod; January 12th, 2006 at 04:48 PM.
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured