편집 기록

편집 기록
  • 프로필 정영훈님의 편집
    날짜2018.06.21

    C++ 복사생성사의 호출


    #include <iostream>
    using namespace std;
    
    class SoSimple
    {
    private:
        int num;
    public:
        SoSimple(int n) : num(n)
        {
            cout << "New Object : " << this << endl;
        }
        SoSimple(const SoSimple& copy) :num(copy.num)
        {
            cout << "New Copy obj: " << this << endl;
        }
        ~SoSimple()
        {
            cout << "Destory obj: " << this << endl;
        }
    };
    
    SoSimple SimpleFuncObj(SoSimple ob)
    {
        cout << "Parm ADR: " << &ob << endl;
        return ob;
    }
    
    int main(void)
    {
        SoSimple obj(7);
        SimpleFuncObj(obj);
    
        cout << endl;
    
        SoSimple tempRef = SimpleFuncObj(obj);
        cout << "Return Obj " << &tempRef << endl;
    
        return 0;
    }
    

    SoSimple tempRef = SimpleFuncObj(obj);

    여기서 tempRef로 SimpleFuncObj(obj)이 복사생성 될줄 알았는데 안그러더라구요.
    이리저리 검색해본결과 컴파일러가 효율을 위해 따로 tempRef를 생성하지않는다라고
    나와있던데 원래 그런건가요 ?
    
  • 프로필 알 수 없는 사용자님의 편집
    날짜2016.05.17

    C++ 복사생성사의 호출


    #include <iostream>
    using namespace std;
    
    class SoSimple
    {
    private:
        int num;
    public:
        SoSimple(int n) : num(n)
        {
            cout << "New Object : " << this << endl;
        }
        SoSimple(const SoSimple& copy) :num(copy.num)
        {
            cout << "New Copy obj: " << this << endl;
        }
        ~SoSimple()
        {
            cout << "Destory obj: " << this << endl;
        }
    };
    
    SoSimple SimpleFuncObj(SoSimple ob)
    {
        cout << "Parm ADR: " << &ob << endl;
        return ob;
    }
    
    int main(void)
    {
        SoSimple obj(7);
        SimpleFuncObj(obj);
    
        cout << endl;
    
        SoSimple tempRef = SimpleFuncObj(obj);
        cout << "Return Obj " << &tempRef << endl;
    
        return 0;
    }
    

    SoSimple tempRef = SimpleFuncObj(obj);

    여기서 tempRef로 SimpleFuncObj(obj)이 복사생성 될줄 알았는데 안그러더라구요.
    이리저리 검색해본결과 컴파일러가 효율을 위해 따로 tempRef를 생성하지않는다라고
    나와있던데 원래 그런건가요 ?