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=
not the subject of this group, that's why I explain that only now): I def=
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=
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 =
But I could also make mandatory to have w2 to derive from an interface
(abstract class) containing setText and readText as pure virtual method; =
this case, as w2 will already derive from some Qt class, we have multiple
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:
virtual void setText(const QString&) = 0;
virtual QString getText() = 0;
extern IHasText* createHasText(params);
class Widget2 : public QSomething, IHasText
// Implement get/setText here
// Implementation of Widget1. E.g.
Widget1::Widget1() : textProvider_(NULL)
textProvider_ = createTextProvider(params);
(Or something to that extent).