If you change
class GLWidget : public QGLWidget, public Widget { }
to
class GLWidget : public virtual QGLWidget { }
you won't achieve the same as in the first example, because any methods exposed by
Widget
will not be available on an instance of
GLWidget
(as they would be in the first example), since you've completely removed
Widget
from the inheritance tree.
To get the initial inheritance tree, but with only one instance of each parent (
virtual
inheritance) you could go for something like this;
#include<iostream>
using namespace std;
class QWidget
{
public:
int value;
QWidget(int param)
{
value = param;
}
virtual ~QWidget() { }
};
class Widget : public virtual QWidget
{
public:
Widget(int paramA, int paramB)
: QWidget(paramA * 10)
{
}
virtual ~Widget() { }
virtual void Foo() { }
};
class QGLWidget : public virtual QWidget
{
public:
QGLWidget(int paramA, int paramB, int paramC)
: QWidget(paramA)
{
}
virtual ~QGLWidget() { }
};
class GLWidget : public virtual QGLWidget, public virtual Widget
{
public:
GLWidget(int paramA, int paramB, int paramC, int paramD)
: QGLWidget(paramA, paramB, paramC), Widget(paramA, paramB), QWidget(45)
{
}
virtual ~GLWidget() { }
};
int main(int argc, char* argv[])
{
GLWidget w(1, 2, 3, 4);
cout << w.value << endl;
w.Foo();
return 0;
}
Notice that you will have to specify constructor arguments all the way down though, this example prints 45 for example (as the constructor of not only
QGLWidget
and
Widget
are called from the
GLWidget
constructor but also that of
QWidget
).
Hope this helps,
Fredrik