[Development] Lack of base classes/interfaces? Q*, Q*F
Kevin Kofler
kevin.kofler at chello.at
Tue Apr 18 00:01:10 CEST 2017
Alejandro Exojo wrote:
> You should really explain which kind of code did you change, because just
> recently I adjusted code from using a key press event to a touch event,
> and the point had to change from QPoint to QPointF, and the changes were
> minimal. This wasn't explained to me, but I just found it natural. Also:
>
> https://wiki.qt.io/API_Design_Principles#Static_Polymorphism
And for the OP who is coming from Python: C++ templates can actually be used
to duck-type things *at compile time*, and thus get some compile-time
polymorphism without an inheritance relationship. So if foo is a template
<typename T>, then calling at(i) on an object of type T will call
QVector::at(int) in foo<QVector> and QList::at(int) in foo<QList>, simply
because of the name. (Trying to instantiate the template with a class that
does not have an at(int) method will of course fail to compile.)
What will of course *NOT* work in C++ is to duck-type things *at runtime*.
You cannot pass a QList to foo<QVector>. Runtime polymorphism is (in
general) only possible through virtual methods and inheritance. (And yes, I
know about the possibilities to (ab)use QMetaObject for Q_OBJECTs or
Q_GADGETs to get there. That is out of scope here.)
Kevin Kofler
More information about the Development
mailing list