我有一个看起来像这样的基类:
class Base
{
public:
typedef std::shared_ptr<Base> ptr_t;
typedef std::weak_ptr<Base> wptr_t;
enum class Type { foo, bar, baz };
Type x;
// ...
};
我希望这些内部类型是公开的,这样我就可以做一些事情,比如 Base::ptr_t my_ptr(new Base);
等等。但是如果我像这样创建一个新类......
class Derived : public Base
{
// ...
};
不幸的是,Derived::ptr_t
仍然是一个基指针。我希望 Derived 从 Base 公开继承 x
,但不继承 ptr_t
、wptr_t
或 Type
。例如
Derived a;
a.x = Base::Type::foo; // this should work
a.x = Derived::Type::foo; // but I want this to fail
这可能吗,也许是对 friend
或 virtual
或类似东西的一些神奇使用?
最佳答案
简单地覆盖类型:
class Derived {
typedef int Type;
};
它将not allow the use of Derived::Type
(因为它是私有(private)的以及 typedef
ed)
关于c++ - 没有继承的公共(public)成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6352606/