Reputation: 426
Suppose I have 3 classes class BaseA
, class BaseB
and class DerivedC
(all forward delcared).
class BaseA
{
public:
BaseA();
BaseA(const BaseB&);
};
and
class BaseB
{
public:
BaseB();
BaseB(const BaseA&);
};
As you can see, class A and B are "symmetrically compatible" (a phrase I invented) so that each can be converted to the other.
class DerivedC: virtual BaseA, virtual BaseB
{
public:
DerivedC();
DerivedC(const BaseA &a): BaseA(a), BaseB(a) {};
DerivedC(const BaseB &b): BaseA(b), BaseB(b) {};
void A() {
(BaseA)*this = (BaseB)*this;
};
};
I was wondering if there was a more elegant/efficient way to do this. I want to set the BaseA part of DerivedC equal to the BaseB part of DerivedC, however these classes are low level and used at a high frequency, and I prefer to only use classes at a high level as a last resort.
Edit: The conversion between A and B involves arctangents and sqrts (can you guess what it is?). But I want to get away from the casts.
Edit: The base classes have some get-sets and operators which should be inherited.
Upvotes: 0
Views: 118
Reputation: 3139
Why not just this:
struct Point { float x,y; };
struct Vector { float mag,theta; };
struct PointVector {
PointVector(const Point& p_,const Vector& v_):p(p_),v(v_) {}
Point p;
Vector v;
};
Upvotes: 1