《深入探索C++对象模型》第六章 执行语意学


#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()中的局部变量销毁所产生。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值