【发布时间】:2011-03-02 12:22:55
【问题描述】:
大家好,
我来自 Java 背景,在多重继承方面遇到困难。
我有一个名为 IView 的接口,它具有 init() 方法。我想派生一个名为 PlaneViewer 的新类,实现上述接口并扩展另一个类。 (QWidget)。
我的实现如下:
IViwer.h(只有头文件,没有 CPP 文件):
#ifndef IVIEWER_H_
#define IVIEWER_H_
class IViewer
{
public:
//IViewer();
///virtual
//~IViewer();
virtual void init()=0;
};
#endif /* IVIEWER_H_ */
我的派生类。
PlaneViewer.h
#ifndef PLANEVIEWER_H
#define PLANEVIEWER_H
#include <QtGui/QWidget>
#include "ui_planeviewer.h"
#include "IViewer.h"
class PlaneViewer : public QWidget , public IViewer
{
Q_OBJECT
public:
PlaneViewer(QWidget *parent = 0);
~PlaneViewer();
void init(); //do I have to define here also ?
private:
Ui::PlaneViewerClass ui;
};
#endif // PLANEVIEWER_H
PlaneViewer.cpp
#include "planeviewer.h"
PlaneViewer::PlaneViewer(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
}
PlaneViewer::~PlaneViewer()
{
}
void PlaneViewer::init(){
}
我的问题是:
- 是否需要在 PlaneViewer 接口中声明 init() 方法,因为它已经在 IView 中定义?
2.我无法编译上面的代码,给出错误:
PlaneViewer]+0x28): 未定义对 `typeinfo for IViewer' 的引用 collect2: ld 返回 1 个退出状态
我是否必须在 CPP 文件中实现 IView(因为我想要的只是一个接口,而不是实现)?
【问题讨论】:
-
我能问一下你们的设计吗?为什么需要在同一个继承层次中将 QWidget 和 IViewer 绑定在一起?你想用多重继承解决什么问题?我问是因为多重继承在一些罕见的情况下很有用,但问题通常以不同的方式得到更好的解决。
-
在我的应用程序中,有几种类型的查看器共享相同的数据。(3D 体素数据)。例如:2D 查看器(XY 平面、YZ 平面、ZX 平面)和 3D 查看器。未来还有会有更多的观众。 QWiget是使用Data的绘制和渲染。 IView 是一个抽象类/接口,用于为所有类型的查看器声明命令方法和数据。
标签: c++ inheritance virtual multiple-inheritance