I'm building a multi-dimensional array using templates but I'm having problems typecasting to a base class. Here is my code so far.
#include <memory>
struct A {
int a;
};
struct B : A {
int b;
};
template<typename T>
struct Array {
std::shared_ptr<T> t;
int length;
T operator[](int x) {
return t[x];
}
Array() {}
template<typename T2>
Array(Array<T2> o) {
t = o.t;
length = o.length;
}
};
int main() {
Array<B> b;
Array<A> a = b; //OK
Array<Array<B>> bb;
Array<Array<A>> aa = bb; //Error
return 0;
}
One-dimension type-cast works, but multi-dimensions fail.
The error I get is
cannot convert 'Array<B>* const' to 'Array<A>*' in assignment
Am I missing an operator or something?
I need something that will work regardless of how many dimensions are created.
The Array class has a lot of functions removed for clarity, I just need the type-cast to work.
Note : the std::shared_ptr::operator[] will require C++17.
Thanks.