简体   繁体   中英

getting a pointer or reference to the base class c++

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.

The conversion between A and B involves arctangents and sqrts (can you guess what it is?). A和B之间的转换涉及反正切和sqrts(您能猜出它是什么吗?)。 But I want to get away from the casts.

The base classes have some get-sets and operators which should be inherited. 基类具有一些应该继承的get-sets和operator。

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;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM