강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

작성자 없음

작성자 정보가 삭제된 글입니다.

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

15.4 std::move

15.4 std::move

작성

·

330

·

수정됨

-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;
이부분에서 컴파일에러가 납니다 강의에서 코드를 빨리 넘겨서 잘 안보여서 어떤게 에러인지 모르겠습니다

답변 1

0

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

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

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

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

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

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

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

작성자 없음

작성자 정보가 삭제된 글입니다.

질문하기