[英]C++ inheritance for abstract classes
I'm a bit new to C++ and trying to work out how to solve something with inheritance. 我对C ++有点陌生,试图尝试如何解决继承问题。
I have generic Open Frameworks app, BaseApp, and then MainApp, a specific implementation of the framework in BaseApp. 我有通用的Open Frameworks应用程序,BaseApp,然后是MainApp,它是BaseApp中框架的特定实现。
An app can render a fullscreen Layer and buffer another Layer for a transition. 应用程序可以渲染全屏图层,并缓冲另一个图层以进行过渡。 Layers also extend from a BaseLayer class, which like BaseApp does nothing until extended.
图层还从BaseLayer类扩展,该类与BaseApp一样,直到扩展。
so 所以
class BaseApp : public ofBaseApp {
public:
BaseLayer renderLayer;
BaseLayer bufferedLayer;
}
// ...
class MainApp : public BaseApp {
public:
VideoLayer videoLayer; // Video + Image layers extend BaseLayer
ImageLayer imageLayer;
}
And what I'd like to do is say that my renderLayer is my imageLayer, and my bufferedLayer is my videoLayer, ie: renderLayer = (some class which extends layer)
so inside my BaseApp I can treat it as a BaseLayer and in my MainApp I can treat it as a VideoLayer, ImageLayer, etc. 我想说的是我的renderLayer是我的imageLayer,我的bufferedLayer是我的videoLayer,即:
renderLayer = (some class which extends layer)
所以在BaseApp中,我可以将其视为BaseLayer和MainApp我可以将其视为VideoLayer,ImageLayer等。
I ended up accomplishing this with shared_ptr
's which seems dirty, but works: 我最终用
shared_ptr
看起来很脏,但可以完成此工作:
class BaseApp : public ofBaseApp {
public:
shared_ptr<BaseLayer> renderLayer;
}
// inside MainApp.setup()
renderLayer = make_shared<ImageLayer>();
if (shared_ptr<ImageLayer> imageLayer = dynamic_pointer_cast<ImageLayer>(renderLayer)){
// ImageLayer specific setup
imageLayer->setup("assets/tiger.png",0,0,800,600);
}
Is there a cleaner way to accomplish this? 有没有更清洁的方法来实现这一目标? I've been trying to stick to oF style, which has as few pointers as possible, so a Layer is a POD class.
我一直在尝试使用oF样式,该样式具有尽可能少的指针,因此Layer是POD类。
I suggest adding virtual
functions to get the render layer and the buffered layer. 我建议添加
virtual
函数以获取渲染层和缓冲层。
class BaseApp : public ofBaseApp {
public:
virtual BaseLayer& getRenderLayer() = 0;
virtual BaseLayer& getBufferedLayer() = 0;
};
// ...
class MainApp : public BaseApp {
public:
virtual BaseLayer& getRenderLayer() { return imageLayer;}
virtual BaseLayer& getBufferedLayer() { return videoLayer; }
private:
VideoLayer videoLayer; // Video + Image layers extend BaseLayer
ImageLayer imageLayer;
};
Add const
where appropriate. 在适当的地方添加
const
。
I am not sure why you need shared_pointer for this, a simple unique_pointer would do nicely. 我不确定为什么您需要shared_pointer,一个简单的unique_pointer会很好。
There are only two ways to handle run-time polymorphic objects in C++ - either through pointer or through reference. 在C ++中,只有两种方法可以处理运行时多态对象:通过指针或通过引用。 References do not seem to fit your design, so you are stuck with pointers.
引用似乎不适合您的设计,因此您被指针所困。 If you do not like pointers, rework the design to not use run-time polymorphism.
如果您不喜欢指针,请重新设计以免使用运行时多态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.