Basti Funck
Basti Funck

Reputation: 1439

Inherit Interface Implementation

I'm new to C++ but have some programming experience in high-level languages (Java, etc.). The main problem I'm currently facing is to understand inheritance and interfaces in 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() { }
};

In my understanding, TestSurface passes all abstract methods from QSurface along. Because of TestWindow is inheriting QWindow which is inheriting QSurface I would expect that all needed methods of TestSurface::QSurface are implemented through TestWindow::QWindow but this isn't the case because I get following error messages upon building:

C2512: C:\...\Qt\Projects\Test2\testwindow.h:10: Erroe: C2512: 'QSurface::QSurface': no appropriate default constructor available

and

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'

Upvotes: 1

Views: 159

Answers (1)

dtech
dtech

Reputation: 49289

That won't work, QWindow is not using virtual inheritance when inheriting QSurface.

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

If you want multiple interfaces to "join a base interface", they all must use virtual inheritance.

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

Upvotes: 3

Related Questions