C/C++|Qt工作笔记-4种方法判断当前对象(类)名或标识(继承发,typeid法,元对象className()法,Q_CLASSINFO法)
生活随笔
收集整理的這篇文章主要介紹了
C/C++|Qt工作笔记-4种方法判断当前对象(类)名或标识(继承发,typeid法,元对象className()法,Q_CLASSINFO法)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
回想起3個月前,剛剛參加工作也做過類似的筆記,但只有2種方法,估計剛畢業(yè)沒有什么墨水,經(jīng)過3個月時間又多了2種方法:
這些方法都可用于RTTI
?
?
第一個方法是繼承發(fā)(C++中很推薦用這個,感覺用這個結(jié)構(gòu)會很清晰):
運行截圖如下:
?
源碼如下:
#include <iostream> #include <assert.h> using namespace std;#define ONEMACRO 0 #define TWOMACRO 1class Base{ public:enum MyType{One,Two,Three,Four,Five,Six};int getMyType()const{return m_type;}void setMyType(int type){m_type=type;}virtual ~Base(){cout<<"~Base() called!"<<endl;}int m_type; };class ImplementOne:public Base{ public:ImplementOne(){setMyType(One);}~ImplementOne(){cout<<"~ImplementOne() called!"<<endl;} };class ImplementTwo:public Base{ public:ImplementTwo(){setMyType(Two);}~ImplementTwo(){cout<<"~ImplementTwo() called!"<<endl;} };void judgment(const Base *object){if(object->getMyType()==ONEMACRO){cout<<"MyType is One"<<endl;/************************************************************************//* want to do sth *//************************************************************************/}else if(object->getMyType()==TWOMACRO){cout<<"MyType is Two"<<endl;/************************************************************************//* want to do sth *//************************************************************************/}else{assert(!"The MyType is unnormal");} }void main(){Base *object1=new ImplementOne;Base *object2=new ImplementTwo;judgment(object1);judgment(object2);delete object1;delete object2;getchar(); }?
第二個方法是typeid法(個人還是不太喜歡用這個,可能是Qt的東西寫多了【Qt中有很多可以替代這種方法】)
運行截圖如下:
源碼如下:
#include <iostream> #include <typeinfo> #include <assert.h> #include <string> using namespace std;class ImplementOne{};class ImplementTwo{};void main(){ImplementOne one;ImplementTwo two;if(strcmp(typeid(one).name(),"class ImplementOne")==0){cout<<"The class name is ImplementOne";}else{cout<<"he he!"<<endl;}getchar(); }?
第三種方法是metaObject()->className()法,這種方法也超級簡單
運行截圖如下:
源碼如下:
metaobject.h
#ifndef METAOBJECT_H #define METAOBJECT_H#include <QObject>class Base:public QObject {Q_OBJECT public:Base(QObject *object=0); };class Child:public QObject {Q_OBJECT public:Child(QObject *object=0); };#endif // METAOBJECT_Hmain.cpp
#include "metaobject.h" #include <QApplication> #include <QMetaObject> #include <QDebug>int main(int argc, char *argv[]) {QApplication a(argc, argv);Base base;Child child;qDebug()<<base.metaObject()->className();qDebug()<<child.metaObject()->className();return a.exec(); }metaobject.pp
#include "metaobject.h"Base::Base(QObject *object):QObject(object) {}Child::Child(QObject *object):QObject(object) {}?
第四種方法也是Qt專有的Q_CLASSINFO法
在此不再重復(fù),
Qt文檔閱讀筆記-Q_CLASSINFO官方解析與實例
https://blog.csdn.net/qq78442761/article/details/83006645
本人的這篇博文已經(jīng)寫出來了!
總結(jié)
以上是生活随笔為你收集整理的C/C++|Qt工作笔记-4种方法判断当前对象(类)名或标识(继承发,typeid法,元对象className()法,Q_CLASSINFO法)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt工作笔记-自定义QSortFilte
- 下一篇: Qt文档阅读笔记-Multicast R