小编Smi*_*ile的帖子

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

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

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 …
Run Code Online (Sandbox Code Playgroud)

c++ multiple-inheritance objective-c++

0
推荐指数
1
解决办法
96
查看次数

标签 统计

c++ ×1

multiple-inheritance ×1

objective-c++ ×1