[Qt-interest] QMetaObject::className() in constructors returns classname of baseclass
Justin Noel
justin at ics.com
Fri May 28 19:50:10 CEST 2010
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: justin.vcf
Type: text/x-vcard
Size: 233 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100528/d1b9745e/attachment.vcf
More information about the Qt-interest-old
mailing list