Nested NameSpaces in C++(C++ 中的嵌套命名空间)
问题描述
当有嵌套的命名空间和对象声明时,我很困惑该怎么做.
I am confused what to do when having nested namespaces and declarations of objects.
我正在移植一些链接到具有一些命名空间的静态库的代码.
I am porting some code that links against a static library that has a few namespaces.
我所说的例子:
namespace ABC {
namespace XYZ {
//STUFF
}
}
如何在代码中声明一个位于命名空间 XYZ
中的对象?
In code what do I do to declare an object that is in namespace XYZ
?
如果我尝试:
XYZ::ClassA myobject;
或:
ABC::XYZ::ClassA myobject;
或:
ABC::ClassA myobject;
我明白了
没有命名类型
错误,即使 ClassA
确实存在.
errors, even though ClassA
definitely exists.
这里有什么合适的?
推荐答案
这取决于你已经在的命名空间:
It depends on the namespace you already are:
如果您不在命名空间或另一个不相关的命名空间中,则必须指定整个路径 ABC::XYZ::ClassA
.
If you're in no namespace or another, unrelated namespace, then you have to specify to whole path ABC::XYZ::ClassA
.
如果您在 ABC
中,则可以跳过 ABC
并直接编写 XYZ::ClassA
.
If you're in ABC
you can skip the ABC
and just write XYZ::ClassA
.
另外,值得一提的是,如果你想引用一个不在命名空间(或根"命名空间)中的函数,你可以在它前面加上 ::
:
Also, worth mentioning that if you want to refer to a function which is not in a namespace (or the "root" namespace), you can prefix it by ::
:
例子:
int foo() { return 1; }
namespace ABC
{
double foo() { return 2.0; }
void bar()
{
foo(); //calls the double version
::foo(); //calls the int version
}
}
这篇关于C++ 中的嵌套命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 中的嵌套命名空间


基础教程推荐
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01