C++:错误:不允许使用间接非虚拟基类
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 的类继承自此并包含文件打开的方法:
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
,您应该没问题。
编译器不允许直接传递,您应该逐步将这些值传递给基类的构造函数目标。
相关文章:
- 在模板基类中为继承类中的可选重写生成虚拟方法
- 子类地址等于虚拟基类地址?
- 是否可以使用基类非虚拟方法中的派生类虚拟方法?
- 虚拟基类函数中派生类的大小
- 使(虚拟)函数在大多数派生类中无法访问中间基类中可访问,定义良好?
- 当键是虚拟继承中涉及的基类指针时,对 std::unordered_map 项的访问崩溃
- 基类可以声明虚拟方法但不定义它吗?仍然在派生类中定义
- 虚拟基类初始化
- googletest:测试基类具有纯虚拟方法的派生类时的核心转储
- 有没有办法在没有虚拟的情况下使用基类指针调用派生类函数
- 我是否需要在虚拟继承类的构造函数中初始化基类以解决菱形继承问题?
- 虚拟基类在内部如何工作?编译器如何解析对基方法的调用?
- 无法使用在子类中定义的虚拟getter实现基类
- C++虚拟函数:基类函数是调用的,而不是派生的
- 关于C++从派生类调用在基类中实现的虚拟函数的问题
- 派生类调用非公共基类虚拟函数
- 使用基类虚拟方法
- 强制调用基类虚拟函数
- 强制调用长链下的基类虚拟函数
- 通过派生类虚拟方法调用基类虚拟方法