如何使用 std::shared_ptr 的多重继承?
How to use multi-inheritence with std::shared_ptr?
所以,我有这样的代码:
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 的子 class' 对象,并且从 IEventable 继承的所有东西都在这里)
类似这样的方法可行:
#include <vector>
#include <memory>
class IUpdatable {
public:
virtual void onUpdate() = 0;
};
class IDrawable {
public:
virtual void onDraw() = 0;
};
class IEventable {
public:
virtual void onEvent() = 0;
};
class IObject {
public:
virtual ~IObject() = default;
};
class Button : public IObject, public IUpdatable, public IDrawable, public IEventable {
public:
void onUpdate() override {}
void onDraw() override {}
void onEvent() override {}
};
class NonVisibleButton : public IObject, public IUpdatable, public IEventable {
public:
void onUpdate() override {}
void onEvent() override {}
};
int main(){
std::vector <std::shared_ptr <IObject>> buttons = {
std::static_pointer_cast<IObject>(std::make_shared<Button>()),
std::static_pointer_cast<IObject>(std::make_shared<NonVisibleButton>())
};
std::vector <std::weak_ptr <IEventable>> eventables = {
std::dynamic_pointer_cast<IEventable>(buttons.at(0)),
std::dynamic_pointer_cast<IEventable>(buttons.at(1))
};
std::vector <std::weak_ptr <IDrawable>> drawbles = {
std::dynamic_pointer_cast<IDrawable>(buttons.at(0))
};
}
老实说,我不会尝试将 Java 代码结构(接口、继承等)塞进 C++ 中...如果可能,请尝试 composition over inheritance。
使用局部变量来保存 Button
和 NonVisibleButton
。这样做的额外好处是不需要 buttons.at
来引用它们。
int main(){
auto b = std::make_shared<Button>();
auto nvb = std::make_shared<NonVisibleButton>();
std::vector <std::shared_ptr <IObject>> buttons = {
b,
nvb
};
std::vector <std::weak_ptr <IEventable>> eventables = {
b,
nvb
};
std::vector <std::weak_ptr <IDrawble>> drawbles = {
b
};
}
所以,我有这样的代码:
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 的子 class' 对象,并且从 IEventable 继承的所有东西都在这里)
类似这样的方法可行:
#include <vector>
#include <memory>
class IUpdatable {
public:
virtual void onUpdate() = 0;
};
class IDrawable {
public:
virtual void onDraw() = 0;
};
class IEventable {
public:
virtual void onEvent() = 0;
};
class IObject {
public:
virtual ~IObject() = default;
};
class Button : public IObject, public IUpdatable, public IDrawable, public IEventable {
public:
void onUpdate() override {}
void onDraw() override {}
void onEvent() override {}
};
class NonVisibleButton : public IObject, public IUpdatable, public IEventable {
public:
void onUpdate() override {}
void onEvent() override {}
};
int main(){
std::vector <std::shared_ptr <IObject>> buttons = {
std::static_pointer_cast<IObject>(std::make_shared<Button>()),
std::static_pointer_cast<IObject>(std::make_shared<NonVisibleButton>())
};
std::vector <std::weak_ptr <IEventable>> eventables = {
std::dynamic_pointer_cast<IEventable>(buttons.at(0)),
std::dynamic_pointer_cast<IEventable>(buttons.at(1))
};
std::vector <std::weak_ptr <IDrawable>> drawbles = {
std::dynamic_pointer_cast<IDrawable>(buttons.at(0))
};
}
老实说,我不会尝试将 Java 代码结构(接口、继承等)塞进 C++ 中...如果可能,请尝试 composition over inheritance。
使用局部变量来保存 Button
和 NonVisibleButton
。这样做的额外好处是不需要 buttons.at
来引用它们。
int main(){
auto b = std::make_shared<Button>();
auto nvb = std::make_shared<NonVisibleButton>();
std::vector <std::shared_ptr <IObject>> buttons = {
b,
nvb
};
std::vector <std::weak_ptr <IEventable>> eventables = {
b,
nvb
};
std::vector <std::weak_ptr <IDrawble>> drawbles = {
b
};
}