1

I have a class Vertex and I want to iterate on its neighbors in particulars ways, like this for example :

Vertex my_v;
for(const Vertex& v : my_v.get_neighbors_closer_than(10.0)
    /* Some code on v*/
// And like this
for(const Vertex& v : my_v.get_neighbors_by_trigonometric_order()
    /* Some code on v*/

For this, I created custom containers and iterators which iterate the way I want. Then, I need to create an inheritate class DlnVertex : public Vertex which have most of the time the same behiavour about iterating functions. Thus, I would like to write inside my DlnVertex class:

DlnVertex my_dv;
for(const DlnVertex& dv : my_dv.get_neighbors_closer_than(10.0))
    /* Some code on dv*/
for(const DlnVertex& dv : get_neighbors_by_trigonometric_order())
    /* Some code on dv*/

My custom iterator returns a Vertex&, and I need in my class DlnVertex to use DlnVertex properties, and I don't know how to avoid re-writing an iterator for my inheritate class because it would have the same behiavour. Is there any advice, patterns, or code examples which can help me ?

Achille
  • 11
  • 1
  • This [question](https://stackoverflow.com/questions/2191572/iterator-for-custom-container-with-derived-classes) may help. – Suthiro Jun 11 '20 at 09:03
  • thank you, I could write something like this, but I'd like to have a **return type of a DlnVertex iterator** in my inherited class for the functions `get_neighbors_closer_than` and the other. – Achille Jun 11 '20 at 11:50

1 Answers1

0

I found a way : Template my base class for the return types of my functions :

template<typename VertexType>
class Vertex
{
    /* ... */
    MyContainer<VertexType> get_neighbors_closer_than(double rad);
}

And then design my derived class by setting itself as the template type :

class DlnVertex : public Vertex<DlnVertex>{/* ... */}
Achille
  • 11
  • 1