#include <iostream>
// operator Type() 类型操作符重载
// operator int()
// operator double()
// ...
//////////////////////////////////////////////////////////
class Rectangle
{
public:
Rectangle(const int w, const int h)
: width(w), height(h)
{};
~Rectangle() {};
operator int(); // 重载 int() 之后,Rectangle就可以像一个int被使用。
public:
int width;
int height;
};
//////////////////////////////////////////////////////////
Rectangle::operator int()
{
return width * height;
}
//////////////////////////////////////////////////////////
void
printInt(const int v)
{
std::cout << v+5 << std::endl;
}
int
main()
{
Rectangle a(40, 10);
int v1 = a; // 400
int v2 = 1 + a; // 401
int v3 = static_cast<int>(a); // 400
std::cout << a << std::endl; // 输出 400
printInt(a); // 输出 405
return 0;
}