c – 没有继承的公共成员
发布时间:2020-12-16 09:35:25 所属栏目:百科 来源:网络整理
导读:我有一个基类,看起来像这样: class Base{public: typedef std::shared_ptrBase ptr_t; typedef std::weak_ptrBase wptr_t; enum class Type { foo,bar,baz }; Type x; // ...}; 我希望那些内部类型是公共的,这样我就可以做像Base :: ptr_t my_ptr(new Base)
我有一个基类,看起来像这样:
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仍然是一个Base指针.我希望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 这是可能的,也许是对朋友或虚拟或类似东西的一些神奇使用? 解决方法
只需覆盖类型:
class Derived { typedef int Type; }; 它将not allow the use of Derived :: Type(因为它是私有的以及typedefed) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |