如何解决多次继承的含糊变量名称?

Smi*_*ile 0 c++ multiple-inheritance objective-c++

所以我有一个以下问题.我有一个类,它是另外两个类的子类,它们都有位置.就像在这个例子中:

struct A
{
    float x, y;
    std::string name;

    void move(float x, float y)
    {
        this->x += x;
        this->y += y;
    }
};

struct B
{
    float x, y;
    int rows, columns;

    void move(float x, float y)
    {
        this->x += x;
        this->y += y;
    }
};

struct C : public A, public B
{
    void move(float x, float y)
    {
        this->x += x; //generates error: "C::x is ambiguous
        this->y += y; //generates error: "C::y is ambiguous
    }
};
Run Code Online (Sandbox Code Playgroud)

在代码的后面,我将C类称为A类和B类,当我得到这个位置时,我遇到了问题.我可以以某种方式"组合"两个类的位置变量吗?如果没有,我可以同时更改两个类的位置,或者我必须这样做:

void move(float x, float y)
{
    this->x1 += x;
    this->y2 += y;
    this->x1 += x;
    this->y2 += y;  
}
Run Code Online (Sandbox Code Playgroud)

提前致谢!

Ton*_*vel 5

在C++中,可以通过在成员变量前面添加类范围来消除歧义:

struct C : public A, public B
{
  void move(float x, float y)
  {
    A::x += x;
    A::y += y;
    B::x += x;
    B::y += y;  
  }
};
Run Code Online (Sandbox Code Playgroud)