다음 코드는 A::A()
만 인쇄하고 A::A(const A&)
또는 operator=
은 인쇄하지 않습니다. 왜?반환 값으로 인해 개체가 복사되지 않는 이유는 무엇입니까?
struct A
{
A() { cout << "A::A()" << endl; }
A(const A& value) { cout << "A::A(const A&)" << endl; }
A& operator=(const A& newValut)
{
cout << "A::operator=" << endl;
return *this;
}
};
A foo()
{
A a; //Ok, there we have to create local object by calling A::A().
return a; //And there we need to copy it, otherwise it will be destroyed
//because it's local object. But we don't.
}
int main()
{
A aa = foo(); //Also there we need to put result to the aa
//by calling A::A(const A&), but we don't.
}
그래서이 코드는
A::A()
A::A(const A&)
A::A(const A&)
를 인쇄해야하지만 그렇지 않습니다. 왜?
foo()
의 인라이닝이 최적화되지 않은 경우 g++
에없는 것이 좋습니다.