...

View Full Version : Template Inheritance w/ Nested Class?



Trinithis
10-22-2007, 06:16 PM
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


#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


#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


#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


#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;
}

oracleguy
10-22-2007, 07:29 PM
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.

Trinithis
10-22-2007, 07:54 PM
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


#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


#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


#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;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum