AllanNabhan
Programmer
Hi !
I have an interesting POO problem using C++. Can somebody help me?
I want to implement a generic interface for sequential containers. I demand all sequential containers (deque, vector, list ...) to derive from a single virtual base class, SequentialContainer. (All classes are actually templatized; I will omit to mention the class parameter). I also want the containers to be able to provide, on demand, an iterator properly initialized on their data. I demand this to be part of the generic interface.
This mean that we need a generic iterator class Iterator, and that the SequentialContainer class provides a method such as:
(1) Iterator SequentialContainer::Iter(void); /* or maybe with initialisation instructions instead of void */
The class Iterator will, by the way, implement a generic interface for iterators, providing for example an incrementation operator.
Now, we want to implement actual sequential containers Deque and List. Of course, they derive from the base class SequentialContainer:
class Deque : public SequentialContainer {
...
};
class List : public SequentialContainer {
...
};
Now, we need the actual iterators. Since it is not at all the same to iterate on a Deque or on a List, the implementation has to be specialized depending on the container. It means that, from the base class Iterator, we must derive subclasses DequeIterator, ListIterator.
And now, we reach my problem: the base class defined the Iter() interface as (1) i.e. returning an Iterator object, not DequeIterator or ListIterator.
It is not elegant to introduce supplementary class specific methods such as:
(2) DequeIterator List::Iterator(void);
when we already have (1). On the other hand, the Iterator base class cannot know about child classes, and we can't have copy constructors from child to parent.
How you would solve this? Is there a simple solution? Or do we need to erase the premise (1)? Or do we need a trick to avoid subclassing Iterator into DequeIterator / ListIterator?
Thank you for your suggestions.
Allan
I have an interesting POO problem using C++. Can somebody help me?
I want to implement a generic interface for sequential containers. I demand all sequential containers (deque, vector, list ...) to derive from a single virtual base class, SequentialContainer. (All classes are actually templatized; I will omit to mention the class parameter). I also want the containers to be able to provide, on demand, an iterator properly initialized on their data. I demand this to be part of the generic interface.
This mean that we need a generic iterator class Iterator, and that the SequentialContainer class provides a method such as:
(1) Iterator SequentialContainer::Iter(void); /* or maybe with initialisation instructions instead of void */
The class Iterator will, by the way, implement a generic interface for iterators, providing for example an incrementation operator.
Now, we want to implement actual sequential containers Deque and List. Of course, they derive from the base class SequentialContainer:
class Deque : public SequentialContainer {
...
};
class List : public SequentialContainer {
...
};
Now, we need the actual iterators. Since it is not at all the same to iterate on a Deque or on a List, the implementation has to be specialized depending on the container. It means that, from the base class Iterator, we must derive subclasses DequeIterator, ListIterator.
And now, we reach my problem: the base class defined the Iter() interface as (1) i.e. returning an Iterator object, not DequeIterator or ListIterator.
It is not elegant to introduce supplementary class specific methods such as:
(2) DequeIterator List::Iterator(void);
when we already have (1). On the other hand, the Iterator base class cannot know about child classes, and we can't have copy constructors from child to parent.
How you would solve this? Is there a simple solution? Or do we need to erase the premise (1)? Or do we need a trick to avoid subclassing Iterator into DequeIterator / ListIterator?
Thank you for your suggestions.
Allan