c++ Multiple parents with same variable name

后端 未结 3 1758
旧时难觅i
旧时难觅i 2020-12-31 08:24
class A{
    protected:
    int var;
};

class B{
    protected:
    int var;
};

class C : public A, public B {};

What happens here? Do the variab

3条回答
  •  鱼传尺愫
    2020-12-31 09:09

    If you only refer to var inside of C, the compiler does not know whether you mean A::var or B::var and the compiler will tell you that var is ambiguous. Therefore, you have to fully qualify the name when using var.

    No merging happens, any instance of C will contain both variables.

提交回复
热议问题