我遇到名称冲突问题。我正在编辑大量类型定义的包装系统,我想避免以下名称冲突:
namespace NS{
struct Interface{};
}
struct OldInterface: private NS::Interface{};
typedef OldInterface Interface;
struct Another : Interface{ // Derived correctly from OldInterface
Another(Interface p){} // C2247 - in struct scope Interface means NS::Interface
};
我试过命名空间——但在对象中它被隐式地切割了。 我也试过私有(private)继承,这导致我又犯了一个错误。
所以问题:如何将它与上述名称一起使用? 例如,如何强制结构内作用域使用命名空间继承名称?
最佳答案
您可以明确声明您想要全局命名空间中的Interface
:
struct Another : Interface{
Another(::Interface p){}
// ^^
};
如果你发现自己经常需要限定这个,你可以为该类型引入一个本地别名:
struct Another : Interface{
using Interface = ::Interface;
//or typedef ::Interface Interface if you can't use C++11
Another(Interface p){}
};
关于C++ 名称与 typedef 别名和继承名称的冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33937600/