程序是這樣的:Comparetor(T a,T b):m_a(a),m_b(b){}int compare(void){cout<<typeid(m_a).name()<<endl;//這句話不明白}
2 回答

蕪湖不蕪
TA貢獻1796條經驗 獲得超7個贊
cout<<typeid(m_a).name()<<endl;//
typeid 是c++語言開啟了RTTI (運行時類型信息)的功能,
這個功能開啟c++會在每個類型上給個typeid 的類,name()是這個類的成員函數

尚方寶劍之說
TA貢獻1788條經驗 獲得超4個贊
typeid 是運算符,檢查 表達式的 類型:
typeid (表達式)
計算返回 type_info 型的 常對象地址,頭文件 <typeinfo>里定義。.name() 返回類型名字。
書上例子:
#include <iostream>
#include <typeinfo>
usingnamespace std;
int main () {
int * a,b;
a=0; b=0;
if (typeid(a) != typeid(b))
{
cout << "a and b are of different types:\n";
cout << "a is: " << typeid(a).name() << '\n';
cout << "b is: " << typeid(b).name() << '\n';
}
return 0;
}
輸出:
a and b are of different types:
a is: int *
b is: int
如果是 class , 它能返回 class 名字。
- 2 回答
- 0 關注
- 178 瀏覽
添加回答
舉報
0/150
提交
取消