日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C/C++|Qt工作笔记-4种方法判断当前对象(类)名或标识(继承发,typeid法,元对象className()法,Q_CLASSINFO法)

發(fā)布時間:2025/3/15 c/c++ 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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_H

main.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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。