Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts

    Template Inheritance w/ Nested Class?

    The scenario:

    I have two templated classes: Iterator and List.

    Inside List, I have a nested ListIterator class that inherits from the abstract class Iterator.

    I want to return the ListIterator as an Iterator through the method list.iterator().

    For simplicity, my List can only contain 1 item, and my ListIterator always returns it via iter.next().

    I can't get this to work.

    -----------------------------

    The code I have so far:

    Iterator.h
    Code:
    #ifndef ITERATOR_H_
    #define ITERATOR_H_
    
    template <class E>
    class Iterator {
       public:
          virtual ~Iterator(){}
          virtual bool hasNext() const = 0;
          virtual E next() const = 0;
    };
    
    #endif
    List.h
    Code:
    #ifndef LIST_H_
    #define LIST_H_
    
    #include "Iterator.h"
    
    template <class E>
    class List {
       private:
          class ListIterator: public Iterator<E> {
             public:
                virtual bool hasNext() const;
                virtual E next() const;
             };
       public:
          E element;
          Iterator<E> iterator() {return ListIterator();}
    };
    
    #include "List.cpp"
    
    #endif
    List.cpp
    Code:
    #include "List.h"
    
    template <class E>
    bool List<E>::ListIterator::hasNext() const {
       return true;
    }
    
    template <class E>
    E List<E>::ListIterator::next() const {
       return element;
    }
    main.cpp
    Code:
    #include <iostream>
    #include "List.h"
    #include "Iterator.h"
    
    int main() {
       List<int> list;
       list.element = 7;
       Iterator<int> iter = list.iterator();
       std::cout << iter.next();
       system("pause");
       return 0;
    }
    Last edited by Trinithis; 10-22-2007 at 07:19 PM.

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    You can't instantiate an abstract class like you are doing in your main.cpp. You need to make it a ListIterator class which will also need to be public as well.
    OracleGuy

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Is there a different way to create this type of polymorphic effect?

    **EDIT**

    Aha! I didn't realize only pointers work with abstract classes. Also, it turns out that the nested class could only access static data.

    (Should I make a deconstructor for List that deletes the ListIterators automatically? If so, should I keep track of the ListIterators using a vector or something? Or simply have the user delete them as needed?)

    List.h
    Code:
    #ifndef LIST_H_
    #define LIST_H_
    
    #include "Iterator.h"
    
    template <class E>
    class List {
       public:
          List(E element);
          Iterator<E>* iterator() {
             return new ListIterator(this);
          }
       private:
          E element;
          class ListIterator: public Iterator<E> {
             public:
                ListIterator(List<E>* list);
                List<E>* list;
                virtual bool hasNext() const;
                virtual E next() const;
          };
    };
    
    #include "List.cpp"
    List.cpp
    Code:
    #include "List.h"
    
    template <class E>
    List<E>::List(E element) {
       this->element = element;
    }
    
    template <class E>
    List<E>::ListIterator::ListIterator(List<E>* list) {
       this->list = list;
    }
    
    template <class E>
    bool List<E>::ListIterator::hasNext() const {
       return true;
    }
    
    template <class E>
    E List<E>::ListIterator::next() const {
       return list->element;
    }
    main.cpp
    Code:
    #include <iostream>
    #include "List.h"
    #include "Iterator.h"
    
    int main() {
       List<int> list(88);
       Iterator<int>* iter = list.iterator();
       std::cout << iter->next();
       delete iter;
       return 0;
    }
    Last edited by Trinithis; 10-22-2007 at 09:25 PM.


  •  

    Posting Permissions

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