15.3강 교수님의 출력 화면에는 출력되지 않는 copy constructor가 생성됩니다.
255
작성한 질문수 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
교수님 결국 copy consturctor와 copy assignment에 대해 추가적으로 공부하여 AutoPtr<Resource> main_res; 이 부분에서 copy constructor가 발생함을 알게 되었습니다. 아직 갈 길이 먼 것 같습니다. 감사합니다!
변수가 메모리에 저장되는 것을 알려주는 강의가 어떤강의였죠
1
466
1
메모리 주소 10진수로 출력
1
653
1
클래스 템플릿 특수화에서 boolalpha로 표현된 리턴값에 대해 질문이 있습니다.
1
499
1
여러가지 리턴 타입에 관한 강의가 어떤 걸까요?
1
534
1
메모리 주소에 관한 질분
0
679
1
인터페이스 클래스에서 reportError의 매개변수에 대해 궁금한 것이 있습니다.
0
549
1
형변환 오버로딩에서 const 관련 질문이 있습니다.
0
443
1
Digit 뒤에 reference를 사용하는 이유
0
510
1
4.2 전역 변수, 정적 변수, 내부 연결, 외부 연결
0
323
1
dat파일이...
0
539
1
TODO:대입 연산자 오버로딩에 대한 소스코드입니다.
0
644
1
복사 생성자 관련 질문이 있습니다.
0
454
1
수업 중 궁금한점이 있습니다.
1
390
1
라이브러리자체가 이해가 되지 않습니다.
0
561
1
마지막 예제 질문
0
302
1
증감연산자 위치에 따른 수행 순서 질문입니다.
0
375
1
단항 연산자 오버로딩에서 return 부분에 질문이 있습니다.
1
412
1
friend함수 관련 질문이 있습니다.
0
312
1
operator+ 정의부분에서 궁금한 것이 있습니다.
0
447
1
3분 17초 질문
0
350
1
함수에 값을 대입한다는 개념이 이해가 되지 않습니다.
0
448
1
int getvalue() const에서 const는 왜 뒤에 붙는건가요?
0
445
2
const Something &st에서 const를 빼면 안되나요?
0
300
1
friend함수는 다른 클래스의 멤버함수로 쓸 수 없나요??
1
493
1





