I'd like to write a group of class D_I (aka I=1,2,... just I mean '_I' to be class proper name, not integer) which handle some (boolean, for example) operations F with same meaning for all classes. But also I want to operate with "sums" of object of such classes, different classes of the group might be "added" together. Mentioned operation of sum of such object depends on corresponding operation of added objects.
That's why I'm going to create common Base class B to handle operation of "summing":
class B
{public:
B (): LEFT(NULL), RIGHT(NULL) {};
B (const SpBrd& S);
B (const B& A, const B B);
~B ();
{if (LEFT != NULL) delete LEFT; // NULL if it is default B or D_I
if (RIGHT != NULL) delete RIGHT; // NULL if it is default B or D_I
}
B operator + (const B& S) const;
bool F(const Point& P) const;
{bool b = aF(P);
if (LEFT != NULL) b = b && LEFT->F(P); // this is how F depends
if (RIGHT != NULL) b = b && RIGHT->F(P); // of added classes
}
protected:
virtual bool aF(const Point& P) const {return true;}; // Default!
B* LEFT; // Pointer to left operand of "sum"
B* RIGHT; // Pointer to right operand of "sum"
// (since it might point to D_i class too as well)
};
so this way derived class D is easy to write since it should only handle it's constructor and aF:
class D_I: public B
{public:
D() {...};
protected:
virtual bool aF (const Point& P) const
{// return something here
}
};
The problem is: HOW TO WRITE operator + and copy constructor of class B:
B:B (const B& S)
{if (S.LEFT != NULL) LEFT = new B (S.LEFT)
if (S.RIGHT != NULL) RIGHT = new B (S.RIGHT)
};
B B:operator + (const B& S) const
{SpBrd S;
S.LEFT = new B (*this);
S.RIGHT = new B (S);
};
would not produce proper result because 'new B ... ' should be substituted to corresponding 'new D_I ... ' if either 'this', 'S' in in 'operator +' or 'S.LEFT' or 'S.RIGHT' in copy constructor are refers not to object of class B exactly but object of one of class D_I.
So, I can't find a way to make B::operator+ know what type sources of sum has since there might be large amount of D_I and they will be added from time to time. What should I do to write this all properly?
I can't write pars of D_I operator+ (D_J) and D_I::D_I(const D_J) for all pairs of (D_I,D_J), it is not fair!
(This would represent something like borders (surface) in space. Function F represent to be inside the border -- i.e. to check if the point is inside a space body. But bodies inside border I want to operate might be intersection (operator+) of space bodies inside other two borders. Examples: intersection of two sphere borders -- inner and outer zones -- produces spherical shell aka 3D ring; might also want to review intersection spherical border with radius R and solid angle of 10*10 degrees around pole and so on.)