复制构造函数不被调用? [英] copy constructor is not called?
本文介绍了复制构造函数不被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class X
{
int i;
public:
X(int m) : i(m) {};
X(const X& x)
{
//cout "copy constructor is called\n";
}
const X opearator++(X& a,int)
{
//cout "X++ is called\n";
X b(a.i);
a.i++;
return b;
}
void f(X a)
{ }
};
int main()
{
X a(1);
f(a);
a++;
return 0;
}
这里当调用函数'f'时,拷贝构造函数会按预期调用。在++的情况下,调用operator ++函数,但是当它返回copy constructor is not called时。
为什么从函数operator ++返回时不会调用copy contructor?
Here when function 'f' is called copy constructor is getting called as expected. In case of a++, operator++ function is called but when it returns "copy constructor is not called". why "copy contructor is not called while returning from function 'operator++'?
推荐答案
返回值优化(RVO)
http://en.wikipedia.org/wiki/Return_value_optimization
I believe you've encountered return value optimization (RVO) http://en.wikipedia.org/wiki/Return_value_optimization
这篇关于复制构造函数不被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文