Use case:
Vector
class (implementing some math) and a derivedVector2D
class- Both classes should ideally allow "copy construction" from each other
Vector
template<std::size_t N, typename T>
class Vector {
public:
// ...
template <typename... TArgs>
Vector(TArgs... args) : data({args...}) {}
Vector(const Vector &other) = default; // copy constructor
// ...
protected:
std::array<T, N> data;
};
Vector2D
template<typename T>
class Vector2D : public Vector<2,T> {
public:
using Vector<2, T>::Vector; // inherit base class constructors
Vector2D(const Vector<2, T>& other) : Vector<2, T>(other) {}
// Vector2D specific functions, e.g. rotation
//...
};
note: the actual classes contain a lot more but i condensed it down to the code that i think is most important here.
The problem is that i'm not able to implement a way such that a Vector
can be constructed from a Vector2D
, see code below. All other cases work fine.
// Example 1 (compiles)
mu::Vector<2, int> a{1, 2};
mu::Vector<2, int> b{a};
// Example 2 (compiles)
mu::Vector2D<int> c{1, 2};
mu::Vector2D<int> d{c};
// Example 3 (compiles)
mu::Vector<2, int> e{1, 2};
mu::Vector2D<int> f{e};
// Example 4 (doesn't compile) <-- how to get this to work?
mu::Vector2D<int> g{1, 2};
mu::Vector<2, int> h{g};
Of course the more general question would be if inheritance is the right way to structure these classes. But i'd like Vector2D
to have all the functionality of Vector
and also additional functions that the Vector
does not have.
Aucun commentaire:
Enregistrer un commentaire