【发布时间】:2014-12-16 00:55:40
【问题描述】:
如果名称空间、函数和变量在同一范围内具有相同的名称,那么它们为什么会发生冲突,而结构/类在与第一个发生冲突时不会与变量和函数发生冲突?
我可以理解为什么结构/类和命名空间会相互冲突,但不会与函数和变量发生冲突,但我觉得奇怪的是,当命名空间发生冲突时,结构/类不会与变量和函数发生冲突,可能是因为它们被用于以相同的方式 (::) 并且都做出了各种命名空间,这将解释它们需要彼此不同(而现在的结果似乎有点无关紧要)。
示例 1:
int A;
struct A {};
//void A() {} //Collision with int A
//namespace A {} //Collision with int A (and also struct A if int A is removed)
示例 2:
struct A {};
void A() {}
//int A; //Collision with function A
//namespace A {} //Collision with function A (and also struct A if int A is removed)
示例 3:
namespace A {}
//struct A {}; //Collision with namespace A
//void A() {} //Collision with namespace A
//int A; //Collision with namespace A (and function A if namespace A is removed)
【问题讨论】:
-
您的
structs 后面需要分号:struct A {};
标签: c++ variables struct namespaces collision