为什么我不能使用 using 来消除基本成员变量之间的歧义?
Why can't I use using to disambiguate between base members variables?
在这个简单的类层次结构中,我试图让类C通过告诉它"使用B::x"来消除使用哪个x的歧义,但这在g++中无法编译,因为它仍然无法找出我在函数foo中指的是哪个x。我知道使用可以用来促进隐藏的方法,但为什么不变量?我考虑过将类X作为a和B的虚基并定义X,但这并不是我想要的;我想要的是A:x被直接从它派生的东西使用,除了从B派生的东西,有点像Python使用其成员(name)解析顺序算法的方式(最后一个类获胜,所以在这种情况下使用B:x,请参阅http://starship.python.net/crew/timehorse/BFS_vs_MRO.html获取描述。)
我在评估ISO c++ 2011在这方面的不足是正确的吗?使用"using"来消除基本成员变量的歧义是不可能的?
class A {
protected:
int x;
};
class B {
protected:
int x;
};
class C : public A, public B {
protected:
using B::x;
public:
int foo(void) { return x; }
};
编辑:编译器版本:g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
在c++ 11和c++ 4.8下运行良好:http://ideone.com/oF4ozq
#include <iostream>
using namespace std;
class A {
protected:
int x = 5 ;
};
class B {
protected:
int x = 42 ;
};
class C : public A, public B {
protected:
using B::x;
public:
int foo(void) { return x; }
int fooa(void) { return A::x; }
int foob(void) { return B::x; }
};
int main() {
C c;
std::cout<<c.foo()<<std::endl;
std::cout<<c.fooa()<<std::endl;
std::cout<<c.foob()<<std::endl;
return 0;
}
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- C++预处理会生成变量成员、资源库和映射
- 局部堆栈变量成员的返回值优化
- Google Mock:在目标类的构造函数中实例化的模拟私有变量成员
- 使用指向结构变量成员的指针访问该结构的成员的地址
- 如果派生类仅包含自动变量成员,是否有必要具有虚拟驱动器
- 线程安全性和静态变量/成员功能
- "static const char array"可以在 C 语言上包含变量成员吗
- 仅用于内部目的的类的所有变量/成员的技术术语是什么
- 如何强制转换变量成员以将其作为函数的引用参数传递
- 类中未声明变量成员函数
- 在 main 中初始化类的 "static const" 类型变量成员的更好方法
- c++模板类静态const变量成员作为映射键给出未定义引用
- 在类中初始化结构变量成员会导致分割错误
- 在c++中建模变量成员类型
- 模板私有静态变量成员的未定义符号
- 不能访问公共静态变量成员
- 常量变量成员在C++有什么用?
- g++ 4.8.2坚持简单变量成员是数组