operatorconversion.cpp
来自「ThinkingC++中文版」· C++ 代码 · 共 26 行
CPP
26 行
class The_Other_One {
int i;
public:
The_Other_One(int ii = 0) : i(ii) {}
};
class The_One {
int x;
public:
The_One(int xx) : x(xx) //把int类型转换成The_One类型
{}
operator The_Other_One() const// 把The_One类型转换成The_Other_One类型,且自动执行
{
return The_Other_One(x); //返回一个The_Other_One对象
}
};
void g(The_Other_One) {}
void main() {
The_One a(1);
g(a);
// g(1); // Calls g( The_Other_One(1) )
} ///:~
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?