0

I have an inheritance structure of objects with begin() and end() as pure virtual member functions in the base class. From this objects I'm planning to build a composite structure. This inner objects have std::vector member the begin() and end() get their data from. But in a leaf class there is no vector. Now I try to find a return value for begin() and end() in the leaf classes. What would be a good way to do that?

The easiest way would be to have a vector member in the leaf classes with no elements in it to fuel begin() and end(), but this just doesn't feel right.

DaClown
  • 4,171
  • 6
  • 31
  • 31
  • Why doesn't it feel right? That seems fairly intuitive to me. – Oliver Charlesworth Feb 03 '11 at 23:31
  • 4
    If not all of the derived classes can meaningfully implement a function, was is that function in the base class? Sounds like the product of bad design, to me. – GManNickG Feb 03 '11 at 23:32
  • Any chance we could see the virtual base class' `begin` and `end` implementations? – Chris Lutz Feb 03 '11 at 23:38
  • What's the specific reason for creating a completely different class for leafs as opposed to nodes? – user470379 Feb 03 '11 at 23:39
  • Might be bad design, I'm not so sure either. I figured since I want to represent a structure through it's parts a composite is the pattern of choice. Now I have several components with the possibility to have children and only one element that shall not have any. What structure would be better to accomplish this? Leafs just close a branch, it makes sense for the whole structure although I could use similar non-leaf class and leave the semantics to ... others. – DaClown Feb 03 '11 at 23:40
  • What's a leaf and an outer class? I've never heard these terms applied to a class hierarchy before. Which one is "base" and which is "Derived"? – SoapBox Feb 04 '11 at 00:02
  • They could return iterators to an empty *static* vector. – UncleBens Feb 04 '11 at 00:07
  • 1
    @SoapBox If you read carefully you will find that 'inner object' and 'leaf classes' refer to the composite pattern. One might expect a leaf class not to have any descendents whereas a inner class has. – DaClown Feb 04 '11 at 08:31

1 Answers1

1

You could implement a very simple iterator class inside your leaf class that simply return dummy iterators. E.g. the begin() returns the same iterator as is returned by end(). You will need to implement some comparison functions as well. I'm not quite sure how std::iterator deals with this, but maybe that has some things you need.

Maister
  • 4,978
  • 1
  • 31
  • 34
  • If `begin()` and `end()` are virtual overrides, they need to return a specific type. So using a dummy class won't help here. – aschepler Feb 03 '11 at 23:33
  • I'll look into this, a custom iterator seems to be a good way to do this. Thanks – DaClown Feb 04 '11 at 08:58