继承接口实现
Inherit Interface Implementation
我是 C++ 的新手,但有一些高级语言(Java 等)的编程经验。我目前面临的主要问题是理解 C++.
中的继承和接口
testsurface.h
#include <QSurface>
class TestSurface : public virtual QSurface
{
public:
TestSurface(SurfaceClass clazz) : QSurface(clazz) { }
~TestSurface() { }
virtual QSurfaceFormat format() = 0;
virtual QSize size() = 0;
virtual QPlatformSurface* surfaceHandle() = 0;
virtual SurfaceType surfaceType() = 0;
};
testwindow.h
#include <QWindow>
#include "testsurface.h"
class TestWindow : public QWindow, public TestSurface
{
public:
TestWindow() : QWindow(), TestSurface(QWindow::Window) { }
~TestWindow() { }
};
据我了解,TestSurface
传递了 QSurface
的所有抽象方法。因为 TestWindow
正在继承 QWindow
而 QWindow
正在继承 QSurface
我希望 TestSurface::QSurface
所有需要的方法都是通过 TestWindow::QWindow
实现的,但事实并非如此因为我在构建时收到以下错误消息:
C2512: C:\...\Qt\Projects\Test2\testwindow.h:10: Erroe: C2512: 'QSurface::QSurface': no appropriate default constructor available
和
C:\...\Qt\Projects\Test2\main.cpp:12: Error: C2259: 'TestWindow': Instance of abstract class can't be created because of following members:
"QSurfaceFormat TestSurface::format(void)": is abstract
c:\...\qt\projects\test2\testsurface.h(12): See declaration of 'TestSurface::format'
"QSize TestSurface::size(void)": is abstract
c:\...\qt\projects\test2\testsurface.h(13): See declaration of 'TestSurface::size'
"QPlatformSurface *TestSurface::surfaceHandle(void)": is abstract
c:\...\qt\projects\test2\testsurface.h(14): See declaration of 'TestSurface::surfaceHandle'
"QSurface::SurfaceType TestSurface::surfaceType(void)": is abstract
c:\...\qt\projects\test2\testsurface.h(15): See declaration of 'TestSurface::surfaceType'
"QSurfaceFormat QSurface::format(void) const": is abstract
D:\...\msvc2013_64_opengl\include\QtGui/qsurface.h(67): See declaration of 'QSurface::format'
"QPlatformSurface *QSurface::surfaceHandle(void) const": is abstract
D:\...\msvc2013_64_opengl\include\QtGui/qsurface.h(68): See declaration of 'QSurface::surfaceHandle'
"QSurface::SurfaceType QSurface::surfaceType(void) const": is abstract
D:\...\msvc2013_64_opengl\include\QtGui/qsurface.h(70): See declaration of 'QSurface::surfaceType'
"QSize QSurface::size(void) const": is abstract
D:\...\msvc2013_64_opengl\include\QtGui/qsurface.h(73): See declaration of 'QSurface::size'
那不行,QWindow
继承QSurface
时没有使用虚拟继承。
class Q_GUI_EXPORT QWindow : public QObject, public QSurface {...};
如果你想要"join a base interface"的多个接口,它们都必须使用虚拟继承。
Base
/ \
/ \
virtual / \ virtual
Der1 Der2
\ /
\ /
\ /
Join
我是 C++ 的新手,但有一些高级语言(Java 等)的编程经验。我目前面临的主要问题是理解 C++.
中的继承和接口testsurface.h
#include <QSurface>
class TestSurface : public virtual QSurface
{
public:
TestSurface(SurfaceClass clazz) : QSurface(clazz) { }
~TestSurface() { }
virtual QSurfaceFormat format() = 0;
virtual QSize size() = 0;
virtual QPlatformSurface* surfaceHandle() = 0;
virtual SurfaceType surfaceType() = 0;
};
testwindow.h
#include <QWindow>
#include "testsurface.h"
class TestWindow : public QWindow, public TestSurface
{
public:
TestWindow() : QWindow(), TestSurface(QWindow::Window) { }
~TestWindow() { }
};
据我了解,TestSurface
传递了 QSurface
的所有抽象方法。因为 TestWindow
正在继承 QWindow
而 QWindow
正在继承 QSurface
我希望 TestSurface::QSurface
所有需要的方法都是通过 TestWindow::QWindow
实现的,但事实并非如此因为我在构建时收到以下错误消息:
C2512: C:\...\Qt\Projects\Test2\testwindow.h:10: Erroe: C2512: 'QSurface::QSurface': no appropriate default constructor available
和
C:\...\Qt\Projects\Test2\main.cpp:12: Error: C2259: 'TestWindow': Instance of abstract class can't be created because of following members:
"QSurfaceFormat TestSurface::format(void)": is abstract
c:\...\qt\projects\test2\testsurface.h(12): See declaration of 'TestSurface::format'
"QSize TestSurface::size(void)": is abstract
c:\...\qt\projects\test2\testsurface.h(13): See declaration of 'TestSurface::size'
"QPlatformSurface *TestSurface::surfaceHandle(void)": is abstract
c:\...\qt\projects\test2\testsurface.h(14): See declaration of 'TestSurface::surfaceHandle'
"QSurface::SurfaceType TestSurface::surfaceType(void)": is abstract
c:\...\qt\projects\test2\testsurface.h(15): See declaration of 'TestSurface::surfaceType'
"QSurfaceFormat QSurface::format(void) const": is abstract
D:\...\msvc2013_64_opengl\include\QtGui/qsurface.h(67): See declaration of 'QSurface::format'
"QPlatformSurface *QSurface::surfaceHandle(void) const": is abstract
D:\...\msvc2013_64_opengl\include\QtGui/qsurface.h(68): See declaration of 'QSurface::surfaceHandle'
"QSurface::SurfaceType QSurface::surfaceType(void) const": is abstract
D:\...\msvc2013_64_opengl\include\QtGui/qsurface.h(70): See declaration of 'QSurface::surfaceType'
"QSize QSurface::size(void) const": is abstract
D:\...\msvc2013_64_opengl\include\QtGui/qsurface.h(73): See declaration of 'QSurface::size'
那不行,QWindow
继承QSurface
时没有使用虚拟继承。
class Q_GUI_EXPORT QWindow : public QObject, public QSurface {...};
如果你想要"join a base interface"的多个接口,它们都必须使用虚拟继承。
Base
/ \
/ \
virtual / \ virtual
Der1 Der2
\ /
\ /
\ /
Join