C++ 名称与 typedef 别名和继承名称的冲突
C++ name collisions with typedef alias and inherited names
我遇到名称冲突问题。我正在编辑大量类型定义的包装系统,我想避免以下名称冲突:
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
};
我试过命名空间 - 但在对象中它被隐式切割。
我也试过私有继承,这又导致了另一个错误。
所以问题:它是如何使用上面的名字的方式吗?
例如,如何强制结构内作用域使用命名空间继承名称?
您可以明确声明您想要来自全局命名空间的 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){}
};
我遇到名称冲突问题。我正在编辑大量类型定义的包装系统,我想避免以下名称冲突:
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
};
我试过命名空间 - 但在对象中它被隐式切割。 我也试过私有继承,这又导致了另一个错误。
所以问题:它是如何使用上面的名字的方式吗? 例如,如何强制结构内作用域使用命名空间继承名称?
您可以明确声明您想要来自全局命名空间的 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){}
};