0
为什么函数func在main函数的最后一行中的返回调用不会被调用复制构造函数..当我通过值发送参数时会调用它,但当我正在返回一个值复制构造函数在函数返回期间未调用
class A
{
public:
int x , y , z;
A(int x=4 , int y=2 , int z=1)
{
this->x = x;
this->y = y;
this->z = z;
}
A(A& a)
{
x = a.x;
y = a.y;
z = a.z;
printf("Copy Constructor called\n");
a.x++;
}
//not a copy constructor
A(A *a)
{
x = a->x;
y = a->y;
z = a->z;
printf("Some Constructor called\n");
(a->x)++;
}
void tell() { printf("x=%d y=%d z=%d\n" , x , y , z);}
};
A func()
{
A a;
return a;
}
int main()
{
A a1;
a1=func(); //why is copy constructor not called while returning
a1.tell();
return 0;
}
阅读有关[复制省略](http://en.wikipedia.org/wiki/Copy_elision)([也在这里(http://en.cppreference.com/w/cpp /语言/ copy_elision))。 –