c ++具有相同变量名的多个父项 [英] c++ Multiple parents with same variable name

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

问题描述

class A{
    protected:
    int var;
};

class B{
    protected:
    int var;
};

class C : public A, public B {};

这里会发生什么?变量合并吗?我可以调用一个特定的,例如B :: var = 2等。

What happens here? Do the variable merges? Can I call one in specific like, B::var = 2, etc.

推荐答案

你上课 C 将有两个变量, B :: var A :: var 。在 C 之外,您可以像这样访问它们(如果您更改为 public:),

You class C will have two variables, B::var and A::var. Outside of C you can access them like this (if you change to public:),

C c;
c.A::var = 2;

尝试访问 c.var 将导致错误,因为没有名称 var 的字段,只有 A :: var B :: var

Attempting to access c.var will lead to an error, since there is no field with the name var, only A::var and B::var.

C 内,它们的行为与常规字段相同,再次,名称 A :: var B :: var

Inside C they behave like regular fields, again, with the names A::var and B::var.

这篇关于c ++具有相同变量名的多个父项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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