命名空间作用域问题
Namespace Scope Question
我有一个关于名称空间作用域的快速问题:
- 我有两个命名空间,A和B,其中B嵌套在A中。 我在a中声明了一些类型定义。
- 我在B中声明了一个类(它在a中)
要从B内部访问类型定义(在A中声明),我是否需要执行"using namespace A;"
ie:B.hpp:
using namespace A;
namespace A {
namespace B {
class MyClass {
typedeffed_int_from_A a;
};
}
}
这似乎是多余的…这是正确的吗?
要从B内部访问类型定义(在A中声明),我是否需要执行"using namespace A;"
。
但是,如果在命名空间B
中定义了与您的typedef同名的其他符号的typedef,那么您需要这样写:
A::some_type a;
让我们做一个简单的实验来理解这个。
考虑以下代码:(必须读取注释)namespace A
{
typedef int some_type; //some_type is int
namespace B
{
typedef char* some_type; //here some_type is char*
struct X
{
some_type m; //what is the type of m? char* or int?
A::some_type n; //what is the type of n? char* or int?
};
void f(int) { cout << "f(int)" << endl; }
void f(char*) { cout << "f(char*)" << endl; }
}
}
int main() {
A::B::X x;
A::B::f(x.m);
A::B::f(x.n);
return 0;
}
输出:f(char*)
f(int)
证明m
的型为char*
, n
的型为int
,符合预期或预期。
在线演示:http://ideone.com/abXc8
不,您不需要using
指令;由于B
嵌套在A
内部,所以A
的内容在B
内部时也在作用域中。
相关文章:
- C++quit()函数中可能存在作用域问题
- 作用域问题:如何从类外查看(不访问也不修改!)私有属性
- C++指针到指针作用域的问题
- 为什么编译器对此模板存在作用域问题
- 将对象的属性传递到方法时出现作用域问题.(opencv相关)
- 全局变量和枚举之间的作用域问题
- 遇到getline、fstream、作用域不一致的问题
- c++双删除文件作用域变量,链接问题
- 类中vector的作用域问题
- 模板作用域问题代码无法编译
- 构造函数排序(全局作用域)问题
- Make_shared在try catch作用域问题中
- c++作用域问题:创建一个矩阵类而不浪费空间
- 关于作用域的一般编程问题
- 当结构未解析为类型和作用域问题时的c++错误
- 名称空间和作用域可见性问题
- 关于从内存中释放链表的析构函数的逻辑和作用域的问题
- C++二进制树编程新节点离开作用域问题
- 在if语句if C++中创建对象时出现作用域问题
- 关于c++中作用域解析操作符的问题