Ami*_*deh 9 c++ syntax namespaces
语法:
using namespace x;
Run Code Online (Sandbox Code Playgroud)
告诉编译器从命名空间x中找到符号.一旦在两个名称空间中具有相同的符号并且您想要相互使用它们,情况就会变得很糟糕.有没有办法告诉编译器不要使用命名空间?我的意思是这样的(命名空间x和y都有函数a)
using namespace x;
int k = a(); //x::a is called
drop namespace x; //imaginary syntax that I am looking for
using namespace y;
int j = a(); //y::a is called
Run Code Online (Sandbox Code Playgroud)
"你必须使用范围解析符号'::'"不是我要找的答案.
jro*_*rok 27
由于您不想使用范围解析,因此请创建一些其他范围:
{
using namespace x;
int k = a(); //x::a is called
}
{
using namespace y;
int j = a(); //y::a is called
}
Run Code Online (Sandbox Code Playgroud)
我担心这可能会比范围解决更糟糕:
编辑:
还有一件事(我不知道,如果你知道的话)可能有用的是命名空间别名.假设你有一个带有令人厌恶的长名称或多个嵌套命名空间的命名空间.您可以缩短名称,例如:
namespace x = very::weird::namespc::name;
namespace y = yabadabadoopdiedoo;
Run Code Online (Sandbox Code Playgroud)
您不能"删除"命名空间,但是,使用命名空间组合时,您可以解决特定的歧义,如下所示:
namespace composite {
using namespace x;
using namespace y;
using y::a; // use y::a in case of ambiguity
void foo() {
a(); // calls y::a
}
}
Run Code Online (Sandbox Code Playgroud)
2 using-directive指定指定命名空间中的名称可以在using-directive之后出现using-directive的范围内使用.在非限定名称查找(3.4.1)期间,名称看起来好像是在最近的封闭命名空间中声明的,其中包含using-directive和指定的命名空间.[注意:在此上下文中,"包含"表示"直接或间接包含". - 尾注]
因此,您必须使用不同的范围.使用后无法取消使用.
| 归档时间: |
|
| 查看次数: |
568 次 |
| 最近记录: |