[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

interacting with elements of a templated container

Posted on 2006-11-08
4
Medium Priority
?
214 Views
Last Modified: 2010-04-02
Hi,

I made a templated container class for testing out templates. It is a simple doubly linked list. Previously I had specified the exact type the container used so there were class specific operations in there, one of which was dumping the contents of the container (just pseudo code):

    class CLinkedList {

         CSpecificClass *m_pLinkedList;

         void DumpToScreen()
         {
              while (m_pLinkedList) {
                   next->PrintContents();
              }
    };

Now CLinkedList uses templates for the data it stores and so what can I do for an operation to dump all the contents of the list since I don't know what the user is going to stick in there? I can't do anything really, can I? I'd have to do it externally now like:

     int main()
     {
          CLinkedList<CWhatever> list;
          list.AddNode(...);
          list.AddNode(...);
          list.AddNode(...);

          // Iterate through each element somehow out here since I know the type only outside, not internally:
          while (somehow iterate externally over the list's elements) {
              *it->Print();
          }
     }

I guess this is why stl implemented the iterators, I'd have to do the same thing?

Thanks
   
0
Comment
Question by:DJ_AM_Juicebox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 17903843
Implementing iterators on your own might be some overkill for startng out, but you could do something like

    template<typename T>
    class CLinkedList {

        public:

        CLinkedList () { // default constructor

            m_pNodes = NULL;
            m_pCurrent = NULL;
        }

        CNode<T>* First() const { m_pCurrent = m_pNodes; return m_pCurrent;}
        CNode<T>* Next() const {

            if (!m_pCurrent) return NULL;

            m_pCurrent = m_pCurrent->m_pNext;

            return m_pCurrent;
        }

        private:
        CNode<T> *m_pNodes;
        CNode<T> *m_pCurrent;
    };

     int main()
     {
          CLinkedList<CWhatever> list;
          list.AddNode(...);
          list.AddNode(...);
          list.AddNode(...);

          // Iterate through each element somehow out here since I know the type only outside, not internally:
         CWhatever<CSomeType>* it = list.First();;
          while (it) {
              it->Print();

              it = list.Next();
          }
     }
0
 
LVL 86

Expert Comment

by:jkr
ID: 17903873
Ooops, remove the 'const' in the above, that should just be

        CNode<T>* First() { m_pCurrent = m_pNodes; return m_pCurrent;}
        CNode<T>* Next() {

            if (!m_pCurrent) return NULL;

            m_pCurrent = m_pCurrent->m_pNext;

            return m_pCurrent;
        }
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 17903918
Yep that's perfect, thanks as always.
0
 
LVL 30

Expert Comment

by:Axter
ID: 17904234
Hi DJ_AM_Juicebox,
> I guess this is why stl implemented the iterators, I'd have to do the same thing?
>>I guess this is why stl implemented the iterators, I'd have to do the same thing?

Are you sure you need a link list.  Rarely is a link list the correct container type, and for most requirements, a std::vector type contianer is much more efficient and more appropriate.
Moreover, with a std::vector, you can use operator[] instead of iterators.

David Maisonave (Axter)
Cheers!
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question