inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

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

15.3 이동 생성자와 이동 대입

[14:55] 교수님 코드와 다르게 move constructor도 호출되었는데요.

337

호두

작성한 질문수 85

1

안녕하세요.

여기서 무브 컨스트럭터는 호출 될 수도 있는 건가요?

리소스의 copy assignment는 사용이 안되는 걸 확인 할 수 있는데

결과가 좀 달려서 여쭤봅니다.

감사합니다.

<메인 전체코드>

// 15_3.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "Timer.h"
#include "AutoPtr.h"
#include "Resource.h"

AutoPtr<ResourcegenerateResource()
{
    AutoPtr<Resource> res(new Resource(10'000'000));

    return res;
}

int main()
{
    using namespace std;
    streambuf* orig_buf = cout.rdbuf();
    //cout.rdbuf(NULL); // disconnect cout from buffer

    Timer timer;

    {
        AutoPtr<Resource> main_res;
        main_res = generateResource();
    }

    cout.rdbuf(orig_buf);
    timer.elapsed();

    return 0;
}

<오토피티알 전체코드>

#pragma once

#include <iostream>
template<class T>
class AutoPtr
{
private:
    T* m_ptr;

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 != nullptrdelete 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)     // preent self-assignment
    //      return *this;

    //  if (m_ptr != nullptr) delete m_ptr;

    //  // deep copy
    //  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;  // really necessary?

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

    AutoPtr& operator=(AutoPtr&& a)
    {
        std::cout << "AutoPtr move assignment " << std::endl;

        if (&a == this) // prevent self-assignment
            return *this;
        
        if (!m_ptr) delete m_ptr;

        // shallow copy
        m_ptr = a.m_ptr;
        a.m_ptr = nullptr;  //really necessary?

        return *this;
    }

};

C++

답변 4

6

audtn7498

release 모드에서는 copy와 move constructor를  실행시키지 않는거 같습니다

2

홍정모

Timer.h와 Resource.h가 없네요.
디버거로 하나하나 찍어보셔야할 것 같습니다.

1

anfdmlrltk11

return 에서 release 랑 debug 차이 때문인 것 같네요

1

홍정모

이게 제가 프로젝트 만들어서 한 번 돌려보고 다시 오겠습니다.

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

1

463

1

메모리 주소 10진수로 출력

1

653

1

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

1

498

1

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

1

534

1

메모리 주소에 관한 질분

0

678

1

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

0

548

1

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

0

443

1

Digit 뒤에 reference를 사용하는 이유

0

509

1

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

0

322

1

dat파일이...

0

537

1

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

0

643

1

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

0

453

1

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

1

389

1

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

0

561

1

마지막 예제 질문

0

302

1

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

0

374

1

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

1

410

1

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

0

311

1

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

0

447

1

3분 17초 질문

0

350

1

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

0

446

1

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

0

442

2

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

0

300

1

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

1

491

1