[Qt-interest] QMetaObject::className() in constructors returns classname of baseclass
Thiago Macieira
thiago at kde.org
Fri May 28 19:47:49 CEST 2010
Em Sexta-feira 28 Maio 2010, às 19:36:21, Niklas Wulf escreveu:
> Hi Qties,
>
> I have some class Foo, which inherits QObject and QDomDocument, and some
> other classes Bar, which inherit Foo. Calling metaObject()->className() in
> Foo's constructor returns "Foo" even it's a instance of Bar. Am I wrong?
No, that is proper C++ behaviour.
> Is this wanted behavior?
Yes, it is.
> Much more important: Shouldn't this be documented?
It is documented in the C++ standard.
During construction and destruction, the current object *is* of the type in
question. So when you say "it's an instance of Bar", that is technically
wrong. It will be an instance of Bar once the Bar constructor is reached. For
the moment, it's just a Foo. And the inverse in destruction: the object ceases
to be of type Bar once its destructor ends.
If you make virtual calls from inside the constructor, they are resolved to
the overrides that exist in Foo, not those in Bar. That's why metaObject()-
>className() returns "Foo" -- it's because the virtual call to metaObject()
was resolved to Foo::metaObject().
You can try any trick in the book (and off the books) to determine what type
the current object is: they will all say it's Foo.
--
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
-------------- 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/20100528/848a853f/attachment.bin
More information about the Qt-interest-old
mailing list