Inheriting nested classes (iterators)

M

Martin

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.
 
V

Victor Bazarov

Martin said:
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
 
M

Martin

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?
 
V

Victor Bazarov

Martin said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top