Re: duct typing and interface in C++
On Jul 24, 11:50 pm, TP <Tribulati...@Paralleles.invalid> wrote:
NOTE: if some (all?) of you understand Qt, this is my precise problem (Qt=
is
not the subject of this group, that's why I explain that only now): I def=
ine
a QWidget w1, which can include a QWidget w2 that must implement some
methods to be used (say: setText, readText). But if w2 is not given, w1
creates a default w2. w2 is a member of w1, in Python no type is needed f=
or
w2, it must only provide setText and readText. But in C++, I must give a
type to w2 in the definition of w1. I could declare w1 as a template for =
w2.
But I could also make mandatory to have w2 to derive from an interface
(abstract class) containing setText and readText as pure virtual method; =
in
this case, as w2 will already derive from some Qt class, we have multiple
inheritance.
Do you understand my problem?
This situation is not specific to Qt. If I were you, I'd do the usual
(or so I think) interface approach:
interface.h
struct IHasText
{
virtual void setText(const QString&) = 0;
virtual QString getText() = 0;
};
factory.h
extern IHasText* createHasText(params);
widget2.h
class Widget2 : public QSomething, IHasText
{
// Implement get/setText here
};
Widget1.h
struct IHasText;
class Widget1
{
IHasText* textProvider_;
};
widget1.cpp
#include "widget.h"
#include "interface.h"
#include "factory.h"
// Implementation of Widget1. E.g.
Widget1::Widget1() : textProvider_(NULL)
{
textProvider_ = createTextProvider(params);
}
(Or something to that extent).
Goran.