Classes and namespaces sharing the same name in C++(在 C++ 中共享相同名称的类和命名空间)
问题描述
假设我在命名空间abc"中有一个名为foo"的类...
Let's say I have a class called 'foo' in namespace "abc"...
namespace abc {
class foo {
int a;
int b;
};
}
...然后说我在不同的命名空间中有另一个名为abc"的类
...and then say I have another class called "abc" in a different namespace
#include "foo.h"
namespace foo {
class abc {
abc::a = 10;
};
}
abc::a 不会是已定义类型,因为它将搜索类 abc,而不是命名空间 abc.我将如何正确引用另一个命名空间中的对象,其中另一个命名空间与我所在的类具有相同的名称?
abc::a would not be a defined type, because it would be searching class abc, not namespace abc. How would I go about properlly referencing an object in another namespace, wherein that other namespace had the same name as the class I'm in?
推荐答案
可以使用::abc::xx,即将变量或类型标识为其绝对命名空间路径.如果您不指定绝对名称,则相对名称在包括命名空间/类中开始向上.
You can use ::abc::xx, that is, identify the variable or type as its absolute namespace path. If you don't specify an absolute name, relative names start going upwards in the including namespaces/classes.
这篇关于在 C++ 中共享相同名称的类和命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C++ 中共享相同名称的类和命名空间
基础教程推荐
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- CString 到 char* 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
