I need a layer of abstraction involving QWidget
that can be QGLWidget
, and I wonder if there is a way to say to the compiler, "Any time you have a doubt (ambiguities) try to use the default base I give you", of course if there is ambiguities it can't resolve with the default choice it prompts errors just like it does. My aim is not have to explicitly solve each of ambiguities one by one since I will always re-direct them to the same class.
Quick setup,
#Qt inheritance (very roughly...)
class QWidget {};
class QGLWidget : public QWidget {};
#my side
class MyAbstract : public QWidget {}; //used by a factory
class MyClass1 : public MyAbstract {};
class MyClass2 : public MyAbstract, public QGLWidget{};
I'am aware compiler can't determine by its-self witch duplicated methods to use for the MyClass2
class, since QGLwidget
inherits and re-implement most of the QWidget
, but can I tell to the complier to use QGLWidget
first since I know that's what I want ?
Qt is just an example here.
I personally doubt that this kind of automatic disambiguation is feasible in C++ at the language level.
What is possible is, case-by-case, disambiguate by explicitly giving the class whose method should be executed, like this:
QGLWidget::ambiguous_method(...
This is not what you are asking for, I know, and I am sure you already know about it. I am saying just for completeness.
On another hand, I am not sure that this kind of automatic disambiguation would be desirable or simply helpful, because the main point about multiple inheritance being "delicate" is the replication of data inside of the derived class. If you had automatic disambiguation, you would end up using sometimes (when there is no ambiguity) the partial object corresponding to a base class, and in other cases the partial object corresponding to the other base class (because of automatic disambiguation) and you would get a mosaic of things that would not make any sense, i.e. a corrupt object...
Finally, I think that this kind of automatic disambiguation would be infeasible in case you have more complex inheritance diagrams, like, following your example:
class Nasty : QGLWidget {};
class Very_nasty : Nasty, MyClass2 {};
There would be no possibility of automatic disambiguation. Indeed, say that the classes you provided form a library and that you decided, when building the library, to use MyClass2::QGLWidget
as a base for disambiguation.
Now, I take your library and define two more classes like the ones I gave. Very_nasty
inherits QGLWidget
from Nasty
and from Class2
; each one has got a QGWidget
inside, and overall I have 3 of them (because Class2 already inherited it twice).
Now suppose that for me, a base class for disambiguation should be Very_Nasty::Nasty::QGLWidget
, given the semantics of my class. If you say that automatic disambiguation is a way to resolve ambiguities with multiple inheritance, I should be able to specify it with each case of multiple inheritance.
What would happen if I call through Very_nasty
a method inherited from MyClass2?
What would happen if I call through Very_nasty
a method inherited from Nasty?
They would take two different disambiguation paths. Clash.