[Qt-interest] QMetaMethod invocation without compile-time knowledge of the return type

Thiago Macieira thiago at kde.org
Fri Sep 25 00:39:02 CEST 2009


Em Quinta-feira 24. Setembro 2009, às 15.51.55, André Fillipe escreveu:
> I just love the moc. :)

This is actually one of the limitations of moc that people dislike.

> To be sure, this "derive primarily from
> QObject" predicate is transitive, right? I mean, if:

Yes.

> class ClassA : public QObject, public AnotherClass, public AndAnother
> {
>     Q_OBJECT
>     // ...
> };
> 
> class ClassB : public ClassA, public GimmeSomeDiamonds
> {
>     Q_OBJECT
>     // ...
> };
> 
> both ClassA and ClassB will have the QObject sub-object at offset zero.

Yes.

The QObject base must be a primary, non-virtual base class of your class, 
directly or indirectly. So if you're not deriving directly from QObject, you 
must be able to reach it via unbroken primary, non-virtual derivations.

The reason for the primary, non-virtual derivation is because virtual or non-
primary derivations cause the QObject sub-object to be located at a non-zero 
offset inside the base class. If that happened, then:

	Author *a = ...;
	QObject *obj = a;

a and obj would have different pointer values, but they would point to the same 
object. The absolute difference between a and obj (reinterpret_cast<char*>(obj) 
- reinterpret_cast<char*>(a)) is a constant if QObject is a non-virtual base 
class. If it were a virtual base class, then the offset must be calculated at 
run-time -- that's also why you can't static_cast up through a virtual 
inheritance.

In an early draft of the Binary Compatibility blog 
(http://labs.trolltech.com/blogs/2009/08/12/some-thoughts-on-binary-
compatibility/) I had an expansion of a C++ virtual derivation done with C 
structures to show what happens. I ended up removing it before publishing.

The QSharedPointer unit test (Qt source code, tests/auto/qsharedpointer) has 
examples of casting to and from classes with secondary inheritance, virtual 
inheritance, etc. Interestingly, the test fails on MSVC 2003 -- it's a 
compiler bug.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358

Qt Developer Days 2009 | Registration Now Open!
Munich, Germany: Oct 12 - 14     San Francisco, California: Nov 2 - 4
      http://qt.nokia.com/qtdevdays2009
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090925/548a3eac/attachment.bin 


More information about the Qt-interest-old mailing list