2013-10-19 5 views
0

주 함수의 마지막 줄에서 함수 func의 반환 호출에 대해 호출 생성자가 호출되지 않는 이유는 무엇입니까? 값을 반환 함함수에서 반환하는 동안 복사 생성자가 호출되지 않습니다.

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; 
    } 
+0

읽기 (http://en.wikipedia.org/wiki/Copy_elision) ([여기] (http://en.cppreference.com/w/cpp/언어/copy_elision)). –

답변

관련 문제