多重继承:相同的变量名称 [英] Multiple Inheritance: same variable name

查看:514
本文介绍了多重继承:相同的变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class A 
{
protected:
string word;
};

class B
{
protected:
string word;
};

类派生:public A,public B
{

};

变量的可访问性如何受影响在派生

解决方案

这将是不明确的,你会得到一个编译错误。 >

您需要使用合适的范围才能使用它:

 派生:public A,public B 
{
Derived()
{
A :: word =A!
B :: word =B !!;
}
};


class A
{
   protected:
    string word;
};

class B
{
   protected:
    string word;
};

class Derived: public A, public B
{

};

How would the accessibility of the variable word be affected in Derived? How would I resolve it?

解决方案

It will be ambiguous, and you'll get a compilation error saying that.

You'll need to use the right scope to use it:

 class Derived: public A, public B
{
    Derived()
    {
        A::word = "A!";
        B::word = "B!!";
    }
};

这篇关于多重继承:相同的变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆