Inheriting nested classes (iterators)

Discussion in 'C++' started by Martin, Jan 21, 2008.

  1. Martin

    Martin Guest

    I have two classes that implements 3D point clouds: M and S. I want
    both classes to implement an iterator to iterate over the points
    contained in each object, and have a points() method to give a pair of
    iterators to the first and the past-the-end element, like this:

    std::pair< M::point_Iterator, M::point_Iterator > M::points();
    std::pair< S::point_Iterator, S::point_Iterator > M::points();

    The iterator implementation needs to be different, since the classes
    store the points in quite different ways. But algorithms working with
    point clouds should work with both classes.

    How should I do this? I can use common inheritance from a base class
    (B) for all other shared methods, of course, but you can't have
    virtual nested classes, right?

    I hope my question is understandable.
    Martin, Jan 21, 2008
    #1
    1. Advertising

  2. Martin wrote:
    > I have two classes that implements 3D point clouds: M and S. I want
    > both classes to implement an iterator to iterate over the points
    > contained in each object, and have a points() method to give a pair of
    > iterators to the first and the past-the-end element, like this:
    >
    > std::pair< M::point_Iterator, M::point_Iterator > M::points();
    > std::pair< S::point_Iterator, S::point_Iterator > M::points();


    I would seem that you made a copy-and-paste mistake here. Don't you
    mean

    std::pair< M::point_Iterator, M::point_Iterator > M::points();
    std::pair< S::point_Iterator, S::point_Iterator > S::points();

    ?

    > The iterator implementation needs to be different, since the classes
    > store the points in quite different ways. But algorithms working with
    > point clouds should work with both classes.
    >
    > How should I do this? I can use common inheritance from a base class
    > (B) for all other shared methods, of course, but you can't have
    > virtual nested classes, right?


    What use would "virtual nested classes" have? What problem could
    you solve with it/them?

    If you go with the base class member, you will have to put the
    type 'Point_Iterator' in the base class as well. Not sure if you
    can do that, nor what problems you're going to have to work around
    if you try to massage your 'B' into having that type.

    > I hope my question is understandable.


    Not fully, but maybe it's just me.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jan 21, 2008
    #2
    1. Advertising

  3. Martin

    Martin Guest

    On 21 Jan, 13:24, Martin <>
    wrote:
    > I have two classes that implements 3D point clouds: M and S. I want
    > both classes to implement an iterator to iterate over the points
    > contained in each object, and have a points() method to give a pair of
    > iterators to the first and the past-the-end element, like this:
    >
    > std::pair< M::point_Iterator, M::point_Iterator > M::points();
    > std::pair< S::point_Iterator, S::point_Iterator > M::points();
    >
    > The iterator implementation needs to be different, since the classes
    > store the points in quite different ways. But algorithms working with
    > point clouds should work with both classes.
    >
    > How should I do this? I can use common inheritance from a base class
    > (B) for all other shared methods, of course, but you can't have
    > virtual nested classes, right?
    >
    > I hope my question is understandable.


    In essence, what I want is a base class B with derived classes M and
    S, and a B::iterator that can be used both with M and S, even though
    their iterator implementations are different. Is that at all possible?
    Martin, Jan 21, 2008
    #3
  4. Martin wrote:
    > On 21 Jan, 13:24, Martin <>
    > wrote:
    >> I have two classes that implements 3D point clouds: M and S. I want
    >> both classes to implement an iterator to iterate over the points
    >> contained in each object, and have a points() method to give a pair
    >> of iterators to the first and the past-the-end element, like this:
    >>
    >> std::pair< M::point_Iterator, M::point_Iterator > M::points();
    >> std::pair< S::point_Iterator, S::point_Iterator > M::points();
    >>
    >> The iterator implementation needs to be different, since the classes
    >> store the points in quite different ways. But algorithms working with
    >> point clouds should work with both classes.
    >>
    >> How should I do this? I can use common inheritance from a base class
    >> (B) for all other shared methods, of course, but you can't have
    >> virtual nested classes, right?
    >>
    >> I hope my question is understandable.

    >
    > In essence, what I want is a base class B with derived classes M and
    > S, and a B::iterator that can be used both with M and S, even though
    > their iterator implementations are different. Is that at all possible?


    Yes. The iterator will have to have a base portion and the "derived"
    portion, most likely of the PIMPL kind. Essentially, you will have to
    make your derived classes provide the iterator's "derived" portion,
    and any functionality in 'B::iterator' will have to be implemented
    through the transparent to the user 'B::iterator_impl', polymorphically.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jan 21, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russ Perry Jr
    Replies:
    2
    Views:
    4,103
    Russ Perry Jr
    Aug 20, 2004
  2. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    478
    Kai-Uwe Bux
    May 8, 2005
  3. Chad E. Dollins
    Replies:
    3
    Views:
    648
    Kai-Uwe Bux
    Nov 8, 2005
  4. Christopher
    Replies:
    7
    Views:
    596
    Christopher
    Apr 4, 2008
  5. , India
    Replies:
    10
    Views:
    1,064
    James Kanze
    Aug 8, 2009
Loading...

Share This Page