#include <iostream>
class X
{
public:
bool operator==(const X& o) { return ((this == &o)?true:false); }
~X() { std::cout << "X::~X()" << std::endl; }
};
class Y
{
public:
operator X() { X x; return x; }
Y getY() { return *this; }
~Y() { std::cout << "Y::~Y()" << std::endl; }
};
int main(int argc, char* argv[])
{
X x;
Y y;
if(x == y.getY())
;
return 0;
}
结果如图所示: 。
main中,代码转换如下:
X x;
Y y;
Y tmp1;
X tmp2;
bool tmp3;
tmp1 = y.getY();
tmp2 = tmp1.operator X();
tmp3 = x.operator==(tmp2);
if(tmp3)
;
tmp2.~X();
tmp1.~Y();
y.~Y();
x.~X();
而第一个X::~X(),则是由于未开始NRV优化,在operator X()中的局部变量销毁所产生。