I have a class A
that implements a function foo
which takes as arguments two iterators (from any stl container). For what I've seen, the common approach is to use a template, as shown here:
class A{
public:
template <typename It>
void foo(const It& begin, const It& end){
// do stuff
}
};
The problem is that I want to make foo
virtual, and templates may not be virtual, but I don't want to have a class template like this:
template <typename It>
class A{
public:
void foo(const It& begin, const It& end){
// do stuff
}
};
because that would limit the use of foo
in a single object to one kind of container.
My question: is there a way to use STL iterators as arguments without using templates, so that is possible to make such functions virtual and keep it working with any STL container?
Edit
Maybe this is a XY problem as said in the comments, so I'm going to explain the specific problem to see what you think.
I have an abstract class called LightSource
with a pure virtual function void castLight(...) = 0
. There are two classes that inherit from it and that implement the function, RadialLight
and DirectedLight
.
The problem is that to cast light I need a collection of objects Edge
that specify segments in a 2D world, that may cast shadow. For the algorithm in castLight
I need to iterate through that collection (not necessarily all of it) and do certain processing that is different in each kind of light.