代码如下,我想要的效果是我想在程序运行时检验一个
实例是不是Son类实例。
现在代码的输出时:
2 QObject S is instance of Class QObject 也就是无法做到我想要的效果。求inherits正确的用法。
- #include <QtGui/QApplication>
- #include "mainwindow.h"
- #include <QList>
- #include <QDebug>
-
- class Father:public QObject
- {
- public:
- Father(){fatherInt =2;}
- int fatherInt;
- };
- class Son:public Father{
- public:
- Son(){sonInt = 1;}
- int sonInt;
- };
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- MainWindow w;
- w.show();
- Son *s = new Son();
- qDebug()<<s->fatherInt;
- qDebug()<<s->metaObject()->className();
- if (s->inherits("Son")){
- qDebug()<<"S is instance of Class Son";
- }
- if (s->inherits("QObject")){
- qDebug()<<"S is instance of Class QObject";
- }
- return a.exec();
- }