【问题标题】:Inherit Interface Implementation继承接口实现
【发布时间】:2015-03-23 23:49:54
【问题描述】:

我是 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 继承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'

【问题讨论】:

    标签: c++ qt inheritance


    【解决方案1】:

    这不行,QWindow 在继承 QSurface 时没有使用虚拟继承。

    class Q_GUI_EXPORT QWindow : public QObject, public QSurface {...};
    

    如果你想让多个接口“加入一个基接口”,它们都必须使用虚拟继承。

               Base
                / \
               /   \
      virtual /     \ virtual
         Der1         Der2
              \     /
               \   /
                \ /
               Join
    

    【讨论】:

    • 嗯,好吧,我明白了。但是,如果我想保留我的设置,那么我将不得不包装 QWindow::QSurface 实现。像 QSize TestWindow::size() { return QWindow::size(); } 一样,但问题是,QWindow::surfaceHandle()QWindow 中是私有的。好像我不能做我想做的事。
    • @BastiFunck - 你可以通过QWindow::handle() 获得QPlatformWindow 的句柄,QPlatformWindow 继承QPlatformSurface,如果你需要获取表面,QSurface *surface() const 具有公共QSurface *surface() const。跨度>
    • 现在我知道我必须采取包装实现的方式,这将是了解如何获取 QWindow 句柄的下一步。感谢您在谷歌搜索上节省了我一些时间
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 2023-03-13
    • 2019-08-04
    相关资源
    最近更新 更多