1.一元构造函数可以作为类型转换函数
2.类型转换函数(成员函数)
语法: operator 类型名() 函数体
功能: 把当前对象转换成"类型名"对象
#include <stdafx.h>
#include <iostream>
using namespace std;
class X
{
public:
X(int a)
{
}
operator int()
{
return 2;
}
};
void main()
{
// error C2440: 'initializing' : cannot convert from 'int' to 'X'
// Constructor for class 'X' is declared 'explicit'
X a = 1;
int b = a;
cout << b << endl;
}