C++深度解析 类型转换函数(下) --- 类类型 转换到 普通类型,类类型之间的转换(41)
类类型转换到普通类型
类型转换函数:
operator Type()
{
Type ret;
// ......
return ret;
}
类型转换函数:
与转换构造函数具有同等的地位
使得编译器有能力将对象转化为其他类型
编译器能够隐式的使用类型转换函数
示例程序:
#include <iostream>
#include <string>
using namespace std;
class Test
{
int mValue;
public:
Test(int i = 0)
{
mValue = i;
}
int value()
{
return mValue;
}
//类型转换函数 Test转换到int
operator int ()
{
return mValue;
}
};
int main()
{
Test t(100);
//t代表100整形值