I want to write a class that manages euclidean vector and that store its initial point using short, int, long or float. I thought to create a template like this:
template<class unit> class EVector
{
private:
unit x;
unit y;
public:
EVector();
setX();
setY();
};
So user creates an EVector choosing the suitable primitive type. But how can I implement the operation between different classes, e.g.
EVector<int> a;
EVector<float> b;
EVector<double> c;
c = a + b;
operator= will copy the coordinates, operator+ adds them.
For addition, you can use my promote
implementation:
template<typename A, typename B>
EVector<typename promote<A, B>::type>
operator +(EVector<A> const& a, EVector<B> const& b) {
EVector<typename promote<A, B>::type> ev;
ev.setX(a.getX() + b.getX());
ev.setY(a.getY() + b.getY());
return ev;
}
For types double
and int
, it will yield double
for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With