inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

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

15.4 std::move

15.4 std::move

334

작성자 없음

작성한 질문수 0

-1

#pragma once

#include<iostream>

template <class T>

class AutoPtr

{

public:

T *m_ptr = nullptr;

AutoPtr(T *ptr = nullptr)

: m_ptr(ptr)

{

std::cout << "auto 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 ) return *this;

if ( m_ptr != nullptr ) delete m_ptr;

m_ptr = new T;

m_ptr = a.m_ptr;

return*this;

}

//AutoPtr(const AutoPtr &a) = delete;

//AutoPtr &operator=(const AutoPtr &a) = delete;

AutoPtr(AutoPtr &&a):m_ptr(a.m_ptr)

{

a.m_ptr = nullptr;

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

}

AutoPtr &operator=(AutoPtr &&a)

{

std::cout << "autoptr move assignment" << std::endl;

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

if ( !m_ptr )delete m_ptr;

m_ptr = a.m_ptr;

a.m_ptr = nullptr;

return*this;

}

T &operator*()const { return *m_ptr; }

T &operator->()const { return m_ptr; }

bool isNull()const { return m_ptr == nullptr; }

};

#pragma once

#include<iostream>

class Resource

{

public:

int *mData = nullptr;

unsigned mLength=0;

Resource()

{

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

}

Resource(unsigned length)

{

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

this->mData = new int[length];

this->mLength = length;

}

Resource(const Resource &res)

{

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

Resource(res.mLength);

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

{

mData[i] = res.mData[i];

//deep copy

}

}

Resource &operator=(Resource &res)

{

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

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

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

mLength = res.mLength;

mData = new int[mLength];

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

{

mData[i] = res.mData[i];

}

return *this;

}

~Resource()

{

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

if ( mData!=nullptr )

{

delete[]mData;

}

}

void Print()

{

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

{

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

}

std::cout << std::endl;

}

};


#include"autoPtr.h"

#include"resource.h"

int main()

{

using namespace std;

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

cout << res1.m_ptr << endl;

AutoPtr<Resource> res2 = res1;

cout << res1.m_ptr << endl;

cout << res2.m_ptr << endl;

}


코드를 따라친거 같은데
AutoPtr<Resource> res2 = res1;
이부분에서 컴파일에러가 납니다 강의에서 코드를 빨리 넘겨서 잘 안보여서 어떤게 에러인지 모르겠습니다

c++ C++

답변 1

0

강민철

안녕하세요, 이는 좋지 못한 질문이라 생각합니다.

  1. 따라 치는 것이 아닌 그 코드를 이해하는 것이 강의의 목적이고

  2. 그 과정에서 발생하는 오류를 해결하는 것 또한 매우 중요한 학습의 일부이며

  3. 발생하는 오류도 매우 기초적인 오류로 강의를 수강하시면 반드시 해결할 수 있어야 하는 오류이기 때문입니다.

오류 메세지를 읽어보시고 구글링 등을 통해 해결을 시도해보시길 권해드립니다.

만일 그래도 해결이 어려우시다면 오류 메세지와 함께

시도하신 코드를 첨부해주시면 답변드리겠습니다.

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

1

470

1

메모리 주소 10진수로 출력

1

655

1

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

1

501

1

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

1

534

1

메모리 주소에 관한 질분

0

679

1

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

0

553

1

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

0

445

1

Digit 뒤에 reference를 사용하는 이유

0

510

1

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

0

324

1

dat파일이...

0

540

1

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

0

646

1

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

0

454

1

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

1

390

1

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

0

562

1

마지막 예제 질문

0

304

1

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

0

378

1

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

1

413

1

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

0

312

1

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

0

447

1

3분 17초 질문

0

352

1

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

0

448

1

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

0

449

2

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

0

301

1

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

1

494

1