• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

15.3강 교수님의 출력 화면에는 출력되지 않는 copy constructor가 생성됩니다.

19.08.02 01:47 작성 조회수 105

1

안녕하세요 교수님! 따배씨 잘 보고 배우고 있습니다.

 

15.3강의 6분 20초쯤 교수님께서 띄우시는 화면에는 바로 AutoPtr default constructor / Resource length constructed / AutoPtr default constructor / 이후에 바로 AutoPtr copy assignment 가 등장하는데요, 제 코드에서는

AutoPtr copy constructor / Resource default constructor / Resource copy assignment / AutoPtr destructor 의 4가지 과정을 거친 후에야 AutoPtr copy assignmnet가 등장합니다. 뒤에 수정하여 적는 AutoPtr move도 마찬가지입니다.

잘못 입력한것인가 싶어 몇 번씩 다시 따라가봤는데도 결과가 똑같아서, 어떤 부분을 놓치고 있는 것인지 답답하여 질문드립니다. 아래에는 AutoPtr.h와 Resource.h, main.cpp를 첨부합니다. 좋은 강의 감사합니다.

AutoPtr.h

#include <iostream>

template<class T>

class AutoPtr

{

private:

T* m_ptr;

public:

AutoPtr(T* ptr = nullptr)

:m_ptr(ptr)

{

std::cout << "AutoPtr default constructor" << std::endl;

}

~AutoPtr()

{

std::cout << "AutoPtr destructor" << std::endl;

if (m_ptr != nullptr) delete m_ptr;

}

AutoPtr(const AutoPtr& a)

{

std::cout << "AutoPtr copy constructor" << std::endl;

//deep copy

m_ptr = new T;

*m_ptr = *a.m_ptr;

}

AutoPtr& operator = (const AutoPtr& a)

{

std::cout << "AutoPtr copy assignment " << std::endl;

if (&a == this) // prevent self-assignment

return *this;

if (m_ptr != nullptr) delete m_ptr;

//deep copy

m_ptr = new T;

*m_ptr = *a.m_ptr;

 

return *this;

}

};

 

Resource.h

#pragma once

#include <iostream>

class Resource

{

public:

int* m_data = nullptr;

unsigned m_length = 0;

public:

Resource()

{

std::cout << "Resource default constructed" << std::endl;

}

Resource(unsigned length)

{

std::cout << "Resource length constructed" << std::endl;

 

this->m_data = new int[length];

this->m_length = length;

}

Resource(const Resource& res)

{

std::cout << "Resource copy constructed" << std::endl;

 

Resource(res.m_length);

 

for (unsigned i = 0; i < m_length; ++i)

m_data[i] = res.m_data[i];

}

~Resource()

{

std::cout << "Resource destroyed" << std::endl;

if (m_data != nullptr) delete[] m_data;

}

Resource& operator = (Resource& res)

{

std::cout << "Resource copy assignment " << std::endl;

if (&res == this) return *this;

if (this->m_data != nullptr) delete[] m_data;

m_length = res.m_length;

m_data = new int[m_length];

for (unsigned i = 0; i < m_length; ++i)

m_data[i] = res.m_data[i];

return *this;

}

void print()

{

for (unsigned i = 0; i < m_length; ++i)

std::cout << m_data[i] << " ";

std::cout << std::endl;

}

 

};

 

main.cpp

 

#include "Timer.h"

#include "AutoPtr.h"

#include "Resource.h"

 

AutoPtr<Resource> generateResource()

{

AutoPtr<Resource> res(new Resource(10000000));

return res;

}

 

int main()

{

using namespace std;

streambuf* orig_buf = cout.rdbuf();

Timer timer;

{

AutoPtr<Resource> main_res;

main_res = generateResource();

}

cout.rdbuf(orig_buf);

timer.elapsed();

return 0;

}

답변 2

·

답변을 작성해보세요.

1

어려운 부분까지 열심히 해오셨네요. 이제 슬슬 따라하면서 입력하고 결과를 확인하는 수준을 넘어서서 디버거로 추적해서 이유를 직접 찾아내셨으면 좋겠습니다.  혹시나 해서 말씀드리는데 r-value reference는 ampersand를 2개 사용합니다. 답답하더라도 포기하지 않고 해결하시길 바래요.

0

지민관님의 프로필

지민관

질문자

2019.08.05

교수님 결국 copy consturctor와 copy assignment에 대해 추가적으로 공부하여 AutoPtr<Resource> main_res; 이 부분에서 copy constructor가 발생함을 알게 되었습니다. 아직 갈 길이 먼 것 같습니다. 감사합니다!