C++名称与typedef别名和继承的名称冲突



我遇到了名称冲突的问题。我正在编辑大规模的typedefed包装系统,我想避免以下名称冲突:

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){}
};

最新更新