我有下面的代码:拷贝构造函数和函数返回的临时
struct balls
{
int mNumBalls;
~balls();
};
inline balls::~balls()
{
// is not called in VS2010 when getBalls returns in monkey constructor
}
balls getBalls()
{
balls myBalls;
myBalls.mNumBalls = 5;
return myBalls;
}
struct monkey
{
balls mBalls;
monkey();
};
inline monkey::monkey() : mBalls(getBalls())
{
}
通过在VS2010调试器步进,我注意到balls
析构函数不是调用时getBalls()
回报在monkey()
构造。这是在C++标准中定义还是仅仅在VC++上存在的一些优化?在这种情况下,我可以依赖不被调用的析构函数跨平台吗?
感谢
'getBalls(){balls myBalls;返回myBalls; }''猴子{球m球; }'...大声笑 – 2013-03-09 20:23:34
更新,开心? :p – KaiserJohaan 2013-03-09 20:24:54
我很高兴与原始版本。任何读“猴子球”的代码都会给我的脸带来微笑。 – 2013-03-09 20:31:11