一、Typeinfo概述
Typeinfo是C++中的一個關鍵字,用於獲取類型信息。它可以返回指向std::type_info對象的指針,這個對象包含了類型的名稱和一些其他信息。
通過Typeinfo,我們可以在程序運行時確定一個對象的類型,並按照其類型進行相應的處理。 Typeinfo最開始是在C++的RTTI(Run-Time Type Identification)標準中引入的,可以作為動態多態的一種實現方式。
使用Typeinfo需要包含頭文件。
二、獲取Typeinfo
我們可以通過對象以及類型名來獲取Typeinfo。
1. 通過對象獲取Typeinfo
#include <typeinfo>
#include <iostream>
using namespace std;
int main() {
int n = 10;
const type_info &info = typeid(n);
cout << info.name() << endl;
return 0;
}
在上面的例子中,我們定義了一個int類型的變量n,然後使用typeid(n)獲取Typeinfo,最後通過.name()方法輸出了變量類型的名稱。在此例中輸出結果為“int”。
2. 通過類型名稱獲取Typeinfo
#include <typeinfo>
#include <iostream>
using namespace std;
int main() {
const type_info &info = typeid(int);
cout << info.name() << endl;
return 0;
}
在這個例子中,我們直接用typeid(int)來獲取int類型的Typeinfo,輸出結果也是“int”,與上一個例子相同。
三、Typeinfo用法
1. 在繼承中使用Typeinfo
#include <typeinfo>
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() {}
};
class Circle : public Shape {
public:
void draw() {
cout << "draw a circle." << endl;
}
};
class Rectangle : public Shape {
public:
void draw() {
cout << "draw a rectangle." << endl;
}
};
int main() {
Circle c;
Shape *s = &c;
if (typeid(*s) == typeid(Circle)) {
s->draw();
}
return 0;
}
在這個例子中,我們先定義了一個基類Shape和兩個派生類Circle和Rectangle,並且重寫了繼承的虛函數draw()。然後我們定義了一個Shape類型的指針s並將其賦值為Circle類型的對象,最後使用typeid(*s)來獲取指針指向對象的Typeinfo,並判斷是否等於Circle類型的Typeinfo。如果相等,就可以通過Circle類型的指針來調用draw()函數。
2. 在模板中使用Typeinfo
#include <typeinfo>
#include <iostream>
using namespace std;
template<typename T>
class Animal {
public:
void whoAmI() {
cout << typeid(T).name() << endl;
}
};
int main() {
Animal<int> a1;
Animal<char> a2;
a1.whoAmI();
a2.whoAmI();
return 0;
}
在這個例子中,我們使用模板來定義Animal類,並使用typeid(T)來獲取模板類型的Typeinfo。在主函數中,我們分別定義了Animal<int>和Animal<char>類型的對象a1和a2,並分別調用了它們的whoAmI()函數,用於輸出它們的類型名稱。
3. 異常類型判斷
#include <typeinfo>
#include <iostream>
#include <exception>
using namespace std;
int main() {
try {
throw std::runtime_error("Exception!");
} catch (const exception &e) {
if (typeid(e) == typeid(runtime_error)) {
cerr << e.what() << endl;
} else {
cerr << "Unknown exception." << endl;
}
}
return 0;
}
在這個例子中,我們使用了C++標準庫中的std::exception和std::runtime_error異常類,並在try-catch語句中捕獲了一些具有std::exception類型的異常。然後使用typeid(e)來獲取捕獲到異常的Typeinfo,並判斷其是否等於std::runtime_error類型的Typeinfo。如果相等,輸出異常信息;否則輸出“Unknown exception.”。
四、實現自己的Typeinfo
我們可以重載運算符“operator==”來實現自定義類型的Typeinfo獲取。
#include <typeinfo>
#include <iostream>
using namespace std;
class MyType {
public:
bool operator==(const type_info &info) const {
return (typeid(MyType) == info);
}
};
int main() {
MyType t;
const type_info &info = typeid(t);
if (info == typeid(MyType)) {
cout << "type matched." << endl;
}
return 0;
}
在這個例子中,我們定義了一個MyType類,並在其中重載了運算符“==”。在運算符中使用typeid(MyType)來獲取MyType類型的Typeinfo,並在調用處使用相等運算符“==”來比較獲取到的Typeinfo和自定義類型的Typeinfo。
五、小結
本文對Typeinfo進行了詳細的闡述和分析。通過多個方面對Typeinfo的用法進行了介紹和示例,包括了通過對象和類型名稱獲取Typeinfo、在繼承中使用Typeinfo、在模板中使用Typeinfo、異常類型判斷和實現自己的Typeinfo。Typeinfo是動態多態的關鍵,並且在許多程序中有重要的作用。在使用Typeinfo時,還需要注意一些細節和限制,比如不能獲取對象的完整信息等。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/250763.html
微信掃一掃
支付寶掃一掃