I am making a GVector class that contains will further derive into 3 types i.e
PVector (Pixel Space Vector)
MVector (Meter Space Vector)
RVector (Rendering Space Vector)
class GVector {
public :
eVectorSpace eVS; // Defines which space the vector would be
float x,y; // The x and y values of a 2-Dimensional vector
...
GVector operator+ (const GVector& v) const { return GVector(x+v.x, y+v.y, v.eVS); }
...
};
class MVector {
public :
PVector toPVector() {...}
//Will contain functions to convert the same vector into a different space
};
I want to make it possible to add 2 vectors lying in the same space
MVector operator+ (const MVector& v) const { return MVector(x+v.x, y+v.y); }
Do I need to make the base class function like this ?
virtual GVector* operator+ (const GVector* v) const () = 0;
But I would like to return a vector in the same space as the two adding vectors.
The function of adding the values of the x,y are same for each type of vector.
Is there a way to minimize this into the base class itself ?
Or is there a better approach to adding vectors in the same space and converting them into different spaces ?
If it makes no sense to perform operations on two different children then the operator should not be defined on the parent. Instead a protected helper function can be defined, and then the children should implement the operator separately, delegating to the helper function.