인프런 커뮤니티 질문&답변
[5:13] 22번줄 main_res = generateResource(); 에서 컴파일러 오류가 나네요 ㅠㅠ
작성
·
280
0
chapter15_03.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();
}
Auto.h
#pragma once
#include <iostream>
template<class T>
class AutoPtr
{
private:
T* m_ptr = nullptr;
public:
AutoPtr(T* ptr = nullptr)
: m_ptr(ptr)
{
std::cout << "AutoPtr defalult constructor" << std::endl;
}
~AutoPtr()
{
std::cout << "AutoPtr destructor" << std::endl;
if (m_ptr != nullptr) delete m_ptr;
}
AutoPtr(AutoPtr& a)
{
std::cout << "AutoPtr copy constructor" << std::endl;
//deep copy
m_ptr = new T;
*m_ptr = *a.m_ptr;
}
AutoPtr& operator = (AutoPtr& a)
{
std::cout << "AutoPtr copy assignment" << std::endl;
if (&a == this)
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 destoryed" << 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;
}
};
Timer.h
#pragma once
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <chrono>
class Timer
{
using clock_t = std::chrono::high_resolution_clock;
using second_t = std::chrono::duration<double, std::ratio<1>>;
std::chrono::time_point<clock_t> start_time = clock_t::now();
public:
void elapsed()
{
std::chrono::time_point<clock_t> end_time = clock_t::now();
std::cout << std::chrono::duration_cast<second_t>(end_time - start_time).count() << std::endl;
}
};
위에 코드로 빌드했을때 컴파일 에러가 나고.
main()문 안에서
main_res = generateResource(); 줄에
no operator "=" matches these operands 라는 오류가 발생합니다.
오버로딩도 잘되었는데 무슨 문제인지 모르겟네요 ㅠㅠ
함수를 통해서 AutoPtr<Resource>를 받는게 안된다는건지...
답변 1
1
안녕하세요!
앞에서 배우셨을 <참조와 const> 강의를 떠올려보시면 이해할 수 있습니다.
generateResource(); 는 함수 리턴값입니다. 즉, 임시 객체인 R-value 입니다.
일반 레퍼런스는 R-value 를 참조할 수 없습니다.
즉, AutoPtr & a 은 R-value 인 함수 리턴 객체를 참조할 수 없다는 것입니다. int & a = 3; 이 안되는 것처럼요! (임시객체 R-value 인 3을 참조할 순 없죠) 일반 레퍼런스는 L-value 성격을 가지는 메모리만 참조할 수 있습니다.
그렇기 때문에 AutoPtr& operator = (AutoPtr& a) 은 호출되지 못합니다. generateResource(); 이 파라미터로 들어갈 수가 없어서요!
반면에 const & 타입은 수정은 안되더라도 L-value, R-value 모두 참조 가능합니다. 따라서 저 generateResource() 를 파라미터로 받는 대입연산자를 호출하기 위해선 AutoPtr& operator = (const AutoPtr& a) 로 하셔야 돼요






감사합니다 L-value R-value를 개념을 잘 알고 있다고 생각했는데 이제야 제대로 이해하네요 ㅠ