inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

홍정모의 따라하며 배우는 C++

15.3 이동 생성자와 이동 대입

[5:13] 22번줄 main_res = generateResource(); 에서 컴파일러 오류가 나네요 ㅠㅠ

292

박승혁

작성한 질문수 7

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>를 받는게 안된다는건지...

C++

답변 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) 로 하셔야 돼요

0

박승혁

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

변수가 메모리에 저장되는 것을 알려주는 강의가 어떤강의였죠

1

481

1

메모리 주소 10진수로 출력

1

672

1

클래스 템플릿 특수화에서 boolalpha로 표현된 리턴값에 대해 질문이 있습니다.

1

515

1

여러가지 리턴 타입에 관한 강의가 어떤 걸까요?

1

550

1

메모리 주소에 관한 질분

0

687

1

인터페이스 클래스에서 reportError의 매개변수에 대해 궁금한 것이 있습니다.

0

558

1

형변환 오버로딩에서 const 관련 질문이 있습니다.

0

448

1

Digit 뒤에 reference를 사용하는 이유

0

512

1

4.2 전역 변수, 정적 변수, 내부 연결, 외부 연결

0

325

1

dat파일이...

0

540

1

TODO:대입 연산자 오버로딩에 대한 소스코드입니다.

0

651

1

복사 생성자 관련 질문이 있습니다.

0

456

1

수업 중 궁금한점이 있습니다.

1

392

1

라이브러리자체가 이해가 되지 않습니다.

0

565

1

마지막 예제 질문

0

305

1

증감연산자 위치에 따른 수행 순서 질문입니다.

0

382

1

단항 연산자 오버로딩에서 return 부분에 질문이 있습니다.

1

416

1

friend함수 관련 질문이 있습니다.

0

313

1

operator+ 정의부분에서 궁금한 것이 있습니다.

0

448

1

3분 17초 질문

0

354

1

함수에 값을 대입한다는 개념이 이해가 되지 않습니다.

0

450

1

int getvalue() const에서 const는 왜 뒤에 붙는건가요?

0

453

2

const Something &st에서 const를 빼면 안되나요?

0

304

1

friend함수는 다른 클래스의 멤버함수로 쓸 수 없나요??

1

495

1