【发布时间】:2021-02-16 10:24:40
【问题描述】:
所以,我有这样的代码:
class IUpdatable {
virtual void onUpdate() = 0;
};
class IDrawable {
virtual void onDraw() = 0;
};
class IEventable {
virtual void onEvent() = 0;
};
class IObject {};
class Button : public IObject, public IUpdatable, public IDrawable, public IEventable {/*override of virtual metothods*/};
class NonVisibleButton : public IObject, public IUpdatable, public IEventable {/*override of virtual methods*/}
int main(){
std::vector <std::shared_ptr <IObject>> buttons = {
new Button(),
new NonVisibleButton()
};
std::vector <std::weak_ptr <IEventable>> eventables = {
buttons.at(0),
buttons.at(1)
};
std::vector <std::weak_ptr <IDrawble>> drawbles = {
buttons.at(0)
};
}
那么,我可以实现这一点以及如何实现吗?我想用不同容器中的按钮定期更新矢量。 (更准确地说,我有单独的线程来更新 IEventable 的子类的对象,绝对所有从 IEventable 继承的东西都在这里)
【问题讨论】:
-
旁白:我认为您不需要无操作类型
IObject,如果您使用std::make_shared,您可以使用std::shared_ptr<void>代替std::shared_ptr<IObject>
标签: c++ stl shared-ptr