[Qt-interest] QMetaObject::className() in constructors returns classname of baseclass
Niklas Wulf
niklas at wulf-home.de
Fri May 28 20:11:09 CEST 2010
Hi,
Thanks for pointing me to that.
I thought of something like that.
And you're right, this really has not to be documented.
Niklas
Am 28.05.2010 um 19:50 schrieb Justin Noel:
> On 05/28/2010 01:36 PM, Niklas Wulf wrote:
>> 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?
>> Is this wanted behavior?
>> Much more important: Shouldn't this be documented?
>>
>> greetings Niklas
>>
>> class Foo() : public QObject, public QDomDocument {
>> Q_OBJECT
>> public:
>> Foo(QObject* parent = 0) : QObject(parent), QDomDocument() { qDebug()<< this->metaObject()->className(); }
>> void test() { qDebug()<< this->metaObject()->className(); }
>> };
>>
>> class Bar : public Foo {
>> Q_OBJECT
>> public:
>> Foo(QObject* parent = 0) : Foo(parent) {}
>> };
>>
>> void main() {
>> Foo* bar = new Bar(/*some QObject**/); //prints "Foo"
>> bar.test(); //prints "Bar"
>> }
>>
>
> Effective C++ Tip #9: Never call virtual functions during construction or destruction
> http://www.artima.com/cppsource/nevercall.html
>
> It's a c++ism and such doesn't need to be documented. Otherwise all virtual functions would need a warning. Check out this book for more good c++ practices: http://www.amazon.com/Effective-Specific-Improve-Programs-Designs/dp/0321334876/ref=sr_1_1?ie=UTF8&s=books&qid=1275068844&sr=8-1
>
> Good Luck!
> --Justin
>
> <justin.vcf>_______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
More information about the Qt-interest-old
mailing list