类或使用声明的枚举
Class or enumeration using declaration
N3797的3.4.3.1/1开头说:
如果限定id的嵌套名称说明符指定了一个类,则类中查找嵌套名称说明符后指定的名称类的范围(10.2),但下列情况除外。
,其中一条规则是:
查找using声明(7.3.3)中指定的名称查找隐藏在同一作用域中的类名或枚举名(3.3.10)。
你能举个例子来说明这个规则吗?
我相信这就是标准所规定的:
struct A {
struct s {} s;
enum e { e };
};
struct B: A {
using A::s;
using A::e;
};
struct B::s s2;
enum B::e e2;
B
作用域中的using-declarations将类名A::s
和枚举名A::e
带入作用域,尽管它们分别由成员和枚举数隐藏。
注意using声明s也将成员和枚举数带入作用域,因此类和枚举仍然隐藏在B
的作用域中;这意味着要在B
或其他地方使用它们,我们需要使用struct
和enum
标签。
在以下代码中,B::m_s
将A::m_s
隐藏在C
中。但它可以通过using A::m_s
直接访问。
注释掉using指令。
#include <iostream>
#include <string>
struct A {
std::string m_s;
A() :
m_s("I am A::m_s")
{}
};
struct B: A {
std::string m_s;
B() :
m_s("I am B::m_s")
{}
};
struct C: B {
using A::m_s;
};
int main() {
C c;
std::cout << 'n' << c.C::m_s << 'n';
}
/*
Local Variables:
compile-command: "g++ -g test.cc -o a.exe && ./a.exe"
End:
*/
相关文章:
- 访问在 C++ 结构中声明的枚举变量
- 如何使用默认值将枚举声明为 extern
- 如何转发声明枚举?
- 如何将枚举类转发声明为模板化类的内部类?
- 枚举前向声明与头文件
- C++:如何在声明枚举类的模板类主体之外定义枚举类?
- C# 使用C++库中声明的枚举
- 函数-本地枚举声明和 ADL 的交互
- 正向声明的枚举,默认值以.h为单位
- 在类访问混淆中声明的枚举类型
- C++ 使用枚举声明:如何导入所有枚举项
- 在枚举声明中C++变量声明
- 枚举声明中的枚举器的类型
- 在C++11中分离枚举声明和定义
- 不透明和匿名枚举声明如何符合标准的要求?
- C++中的Typedef枚举声明
- 此枚举声明是否符合标准?
- 枚举声明错误
- 多个枚举声明
- 这个冒号在枚举声明中的作用