Accessing Iterator as private member of a class

This is a discussion on Accessing Iterator as private member of a class within the C++ Programming forums, part of the General Programming Boards category; My question is how can I access an iterator that is a private member of a class? Code: class Sequence ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    34

    Accessing Iterator as private member of a class

    My question is how can I access an iterator that is a private member of a class?

    Code:
    class Sequence
    {
      friend ostream& operator<<(ostream& OUTPUT, const Sequence & s);
      
      public:
       
        ...
    
      private:
      list <int> intList;
      list <int> :: iterator ListIt;
    }
    
    ...
    
    ostream& operator<<(ostream& OUTPUT, const Sequence&  s)
    {
      for(ListIt=s.intList.begin(); ListIt != intList.end(); ListIt++)
     {
      cout << *ListIt;
     }
     return OUTPUT;
    }
    I have tried the above, and several other ways and I keep getting a compilier error that ListIt is undeclared. Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Use Sequence::ListIt instead of ListIt. Oh, and you need to name your iterator in the loop and dereference it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    And use ++it instead of it++.
    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

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Whilst including an iterator in a class may be a good idea at some times, it serves no purpose in this instance - class members should be "the minimum necessary to get the work done".

    Having unnecessary class members causes two problems:
    1. class bloat - each class instance will take up extra space everywhere it's being stored, copied, etc.
    2. It "dirties the class", meaning that the class implementation is no longer "clean" or "neat". You are mixing in stuff that is "related, but not part of the object you are dealing with".

    Unless I've completely misunderstood something, you should be able to use "ListIt" as a local variable inside your operator<< function, and there's no need to have it inside the class.

    A case where you may need to use this would be if you for some reason need to walk the list of elements as one step at a time, e.g:
    Code:
    class Sequence
    {
      public:
    ...
      int getFirstElement() { ListIt = s.intList.begin(); return *ListIt; };
      int getNextElement() { if (++ListIt == s.intList.end() return 0; else return *ListIt; };
    ...
      private:
      list <int> intList;
      list <int> :: iterator ListIt;
    };

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can Nested class access parent class's private member?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 09:42 AM
  2. Replies: 3
    Last Post: 04-10-2009, 03:20 AM
  3. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  4. Replies: 6
    Last Post: 04-27-2006, 11:55 AM
  5. Accessing private data members
    By maloy in forum C++ Programming
    Replies: 11
    Last Post: 10-04-2002, 03:48 PM

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