5:10 부분 결과값이 강의와 약간 다르게 나오는데 원인이 뭔지 모르겠습니다
218
작성한 질문수 7
main.cpp
#include "AutoPtr.h"
#include "Resource.h"
using namespace std;
AutoPtr<Resource> generateResource()
{
AutoPtr<Resource> res(new Resource(3));
return res;
}
int main()
{
{
AutoPtr<Resource> main_res;
main_res = generateResource();
}
return 0;
}
////////////////////////////
AutoPtr.h
#pragma once
#include <iostream>
template<class T>
class AutoPtr
{
public:
T* m_ptr = nullptr;
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;
}
T& operator*() const { return *m_ptr; }
T* operator->()const { return m_ptr; }
bool isNull()const { return m_ptr == nullptr; }
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 assignement" << std::endl;
if (&a == this)
return *this;
//deep copy
m_ptr = new T;
*m_ptr = *a.m_ptr;
return *this;
}
/////////////////////////////////////
Resource.h
#pragma once
#include <iostream>
class Resource
{
//private:
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()
{
std::cout << "Resource destroyed" << std::endl;
if (m_data != nullptr)
delete[]m_data;
}
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& 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;
}
};
웬만하면 코드 질문은 스스로 해결하려고 했는데 며칠 고민하면서 디버거로 찍어봐도 중간중간에 뜬금없이 생성자가 호출되거나 다시 복사가 되는 원인을 못찾겠습니다
답변 1
8
Debug 모드로 실행하셨기 때문에 그렇습니다. 강의에선 교수님께서 Debug 모드가 아닌 Release 모드로 실행하고 계신데 Debug 모드에선 generateResource 함수가 리턴하는 res 가 임시 객체에 복사되는 과정에서 복사 생성자를 호출 해주는 듯 합니다. 이와 달리 Release 모드로 실행할 땐 res 가 임시 객체에 복사되는 과정에서 복사생성자가 호출되지 않네요.
(함수의 리턴값은 R-value라는 것을 알고 계실겁니다. 리턴값이 임시 공간에 사본으로서 복사된 후 함수 실행이 끝나면서 진짜 리턴값은 사라지죠! 이 R-value 사본을 함수 바깥에서 받게되는 것이구요)
저도 평소에 Debug 모드로 놓고 쓰는지라 이유를 모르겠어서 의아해하다가 https://www.inflearn.com/questions/22630 이 질문글의 댓글 보고 알게되었습니다.
위 링크를 읽어보시면 이해에 도움이 되실 것 같습니다. C++컴파일러는 경우에 따라 복사생성자 호출을 스킵하도록 최적화가 되있는데 디버그 모드보다 더 최적화를 추구하는 릴리즈 모드에선 함수 리턴 값이 임시객체에 복사되는 과정에서의 복사생성자 호출은 아예 생략해주는 것 같네요.
디버그 모드에선 저런 결과가 도출되는 것이 지극히 정상적인 현상이니 걱정안하셔도 될 것 같습니다! 릴리즈 모드로 실행하시면 교수님과 동일한 출력 결과 얻으실 수 있을 것입니다.
변수가 메모리에 저장되는 것을 알려주는 강의가 어떤강의였죠
1
463
1
메모리 주소 10진수로 출력
1
653
1
클래스 템플릿 특수화에서 boolalpha로 표현된 리턴값에 대해 질문이 있습니다.
1
498
1
여러가지 리턴 타입에 관한 강의가 어떤 걸까요?
1
534
1
메모리 주소에 관한 질분
0
678
1
인터페이스 클래스에서 reportError의 매개변수에 대해 궁금한 것이 있습니다.
0
548
1
형변환 오버로딩에서 const 관련 질문이 있습니다.
0
442
1
Digit 뒤에 reference를 사용하는 이유
0
509
1
4.2 전역 변수, 정적 변수, 내부 연결, 외부 연결
0
322
1
dat파일이...
0
537
1
TODO:대입 연산자 오버로딩에 대한 소스코드입니다.
0
642
1
복사 생성자 관련 질문이 있습니다.
0
453
1
수업 중 궁금한점이 있습니다.
1
388
1
라이브러리자체가 이해가 되지 않습니다.
0
560
1
마지막 예제 질문
0
301
1
증감연산자 위치에 따른 수행 순서 질문입니다.
0
373
1
단항 연산자 오버로딩에서 return 부분에 질문이 있습니다.
1
410
1
friend함수 관련 질문이 있습니다.
0
310
1
operator+ 정의부분에서 궁금한 것이 있습니다.
0
446
1
3분 17초 질문
0
350
1
함수에 값을 대입한다는 개념이 이해가 되지 않습니다.
0
445
1
int getvalue() const에서 const는 왜 뒤에 붙는건가요?
0
441
2
const Something &st에서 const를 빼면 안되나요?
0
299
1
friend함수는 다른 클래스의 멤버함수로 쓸 수 없나요??
1
491
1





