![](/img/trans.png)
[英]Is the C++ core guideline C35 "A base class destructor should be either public and virtual, or protected and nonvirtual" wrong for interface classes?
[英]C++: Error: Indirect nonvirtual base class is not allowed
我正在尝试用 C++ 和 OpenGL 创建某种基本的 UI。 有了这个,我使用了一个名为 OGLRectangle 的类:
class OGLRectangle : public Renderable, public Listener
{
public:
OGLRectangle();
OGLRectangle(float cX, float cY);
~OGLRectangle();
...
}
这是由一个 Button 类继承的,该类包含所有按钮类型之间的共享方法:
class Button : public OGLRectangle
最后 ButtonBrowse 类继承自 this 并包含打开文件的方法:
class ButtonBrowse : public Button
{
public:
ButtonBrowse(float cX, float cY);
...
}
现在我说要在 ButtonBrowse 中传递构造函数的参数是正确的,我需要在构造函数中执行以下操作:
ButtonBrowse::ButtonBrowse(float cX, float cY) : OGLRectangle(cX, cY)
{
...
}
如果是这样,为什么我会收到标题中的间接非虚拟错误?
您需要调用Button
的构造函数,然后它将调用OGLRectangle
构造函数。
ButtonBrowse::ButtonBrowse(float cX, float cY) : Button(cX, cY)
{
...
}
只要Button
的构造函数设置为将参数传递给它的直接基类OGLRectangle
,你应该没问题。
这是编译器不允许直接传递的,您应该一步一步将这些值传递给基类的构造函数目的地。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.