[abc e = a + b]가 호출되면 복사 생성자가 호출되지 않습니다.다음과 같은 경우 오버로드 된 + 및 = 연산자가 값별로 반환되는 경우 복사 생성자가 호출되지 않습니다.
class abc{
int i;
public:
abc()
{
i = 10;
cout<<"constructor"<<endl;
}
abc(const abc &a)
{
cout<<"copy constructor"<<endl;
i = a.i;
}
abc operator=(const abc &a)
{
cout<<"operator="<<endl;
abc temp;
temp.i = a.i;
return temp;
}
abc operator+(abc& a)
{
cout <<"Operator+ called"<<endl;
abc temp;
temp.i = i+a.i;
return temp ;
}
};
int main()
{
abc a,b;
cout <<"----------------------------------------------"<<endl;
a = b;
cout <<"----------------------------------------------"<<endl;
abc c = a;
cout <<"-----------------------------------------------"<<endl;
abc d(a);
cout <<"-------------------------------------------"<<endl;
**abc e = a+b;**
}
는 그러나 방법은 클래스 ABC의 객체에 대한 참조를 반환 다음과 같은 방법으로 대체됩니다 과부하 사업자, 생성자를 복사 할 경우 호출됩니다.
abc& operator=(const abc &a)
{
cout<<"operator="<<endl;
i = a.i;
return *this;
}
abc& operator+(const abc& a)
{
cout <<"Operator+ called"<<endl;
i = i+a.i;
return *this ;
}
왜 이런 일이 발생하는지 설명해주세요.
가능한 복제본 [반환 값 최적화 (RVO) 버그가 아닙니까?] (http://stackoverflow.com/questions/3905869/isnt-return-value-optimization-rvo-a-bug) – iammilind
빈약 한 클래스 이름. 변수 이름이'a''b''와'c' 일 때'MyClass','TestClass' 또는 다른 이름으로 호출 할 수 있지만'abc'로 호출 할 수는 없습니다. – abcdabcd987