• 4536阅读
  • 0回复

[提问]求助inherits的用法。 [复制链接]

上一主题 下一主题
离线xyqlgpp
 
只看楼主 倒序阅读 楼主  发表于: 2011-04-20
代码如下,我想要的效果是我想在程序运行时检验一个实例是不是Son类实例。
现在代码的输出时:
2
QObject
S is instance of Class QObject
也就是无法做到我想要的效果。求inherits正确的用法。

  1. #include <QtGui/QApplication>
  2. #include "mainwindow.h"
  3. #include <QList>
  4. #include <QDebug>
  5. class Father:public QObject
  6. {
  7. public:
  8.     Father(){fatherInt =2;}
  9.     int fatherInt;
  10. };
  11. class Son:public Father{
  12. public:
  13.     Son(){sonInt = 1;}
  14.     int sonInt;
  15. };
  16. int main(int argc, char *argv[])
  17. {
  18.     QApplication a(argc, argv);
  19.     MainWindow w;
  20.     w.show();
  21.     Son *s = new Son();
  22.     qDebug()<<s->fatherInt;
  23.     qDebug()<<s->metaObject()->className();
  24.     if (s->inherits("Son")){
  25.         qDebug()<<"S is instance of Class Son";
  26.     }
  27.     if (s->inherits("QObject")){
  28.         qDebug()<<"S is instance of Class QObject";
  29.     }
  30.     return a.exec();
  31. }

快速回复
限100 字节
 
上一个 下一个