consider the following algorithm with arrays:
class MyType;
{
// some stuff
}
class MySubType:MyType
{
// some stuff
}
void foo(MyType** arr, int len)
{
for (int i = 0;i<len;i++)
// do something on arr[i]->
}
void bar()
{
MySubType* arr[10];
// initialize all MySubType*'s in arr
foo(&arr, 10);
}
Nothing too fancy here. My question is - how do I do this with templates?
void foo(std::vector<MyType>& s)
{
std::vector<MyType>::iterator i;
for (i = s.begin(); i != s.end(); i++)
// do stuff on *i
}
so, in bar, I can't do this:
void bar()
{
std::vector<MySubType> s;
foo(s); // compiler error
}
error: invalid initialization of reference of type std::vector<MyType, std::allocator<MyType> >&
from expression of type std::vector<MySubType, std::allocator<MySubType> >
Is there some way to do something like this?
Basically, if there's a way to do this:
std::vector<MySubType> s;
std::vector<MyType>& t = s;
I'd be happy...