C++ 깊은 복사(Deep Copy)

2021. 3. 15. 23:30·C++/Syntax

깊은 복사(Deep Copy)

  • 하위 항목을 모두 복사하는 것

    #include <iostream>
    #include <cassert>
    
    class MyString
    {
      char*    data_ = nullptr;
      int        len_ = 0;
    
    public:
      MyString(const char* source = "")
      {
        assert(source);
    
        len_ = std::strlen(source) + 1;
        data_ = new char[len_];
    
        for (int i = 0; i < len_; ++i)
          data_[i] = source[i];
        data_[len_ - 1] = '\0';
      }
    
      ~MyString()
      {
        delete[] data_;
      }
    
      MyString(const MyString& source)
      {
        std::cout << "Copy constructor\n";
    
        len_ = source.len_;
    
        if (source.data_ != nullptr)
        {
          data_ = new char[len_];
    
          for (int i = 0; i < len_; ++i)
            data_[i] = source.data_[i];
        }
        else
          data_ = nullptr;
      }
    
      char*& getString() { return data_; }
    };
    
    int        main()
    {
      using namespace std;
    
      MyString    hello("Hello");
    
      cout << (int*)hello.getString() << endl;
      cout << hello.getString() << endl;
    
      {
        MyString    copy = hello;
    
        cout << (int*)copy.getString() << endl;
        cout << copy.getString() << endl;
      }
    
      cout << hello.getString() << endl;
    }
    • 얕은 복사에서 발생했던 문제가 발생하지 않는다.
저작자표시 (새창열림)

'C++ > Syntax' 카테고리의 다른 글

C++ 람다 함수 (Lambda Function)  (0) 2021.03.30
C++ delete  (0) 2021.03.15
C++ 얕은 복사(Shallow Copy)  (0) 2021.03.15
C++ 함수 포인터 (Function Pointer)  (0) 2021.03.12
C++ 인라인 함수 (Inline Function)  (0) 2021.03.12
'C++/Syntax' 카테고리의 다른 글
  • C++ 람다 함수 (Lambda Function)
  • C++ delete
  • C++ 얕은 복사(Shallow Copy)
  • C++ 함수 포인터 (Function Pointer)
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 분류 전체보기 (317)
      • Algorithm (13)
        • 알기 쉬운 알고리즘 (10)
        • Search (1)
        • Sort (2)
      • Arduino (0)
      • C++ (185)
        • Class (46)
        • Exception (6)
        • Library (51)
        • Overloading (10)
        • SmartPointer (5)
        • Syntax (33)
        • TBC++ (23)
        • Templates (9)
        • VisualStudio (2)
      • Embedded (1)
      • Git (4)
      • Java (5)
      • Linux (16)
        • Error (1)
        • Linux Structure (11)
      • MacOS (7)
      • OS (1)
        • Concurrency (1)
      • Python (21)
        • Class (1)
        • Function (2)
        • Syntax (17)
      • Raspberrypi (9)
      • Review (1)
      • Utility (12)
        • VSCode (5)
        • VirtualBox (3)
      • Web (8)
        • Nginx (1)
        • React (3)
        • Django (1)
      • Windows (20)
        • Registry (3)
        • WSL (1)
        • DeviceDriver (6)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    Windows 11
    spring
    EXCLUDE
    백기선
    맥북 카카오톡 알림 안뜸
    스프링
    SunOS 5.1
    시스템 복구
    citrix workspace
    java
    windows
    윈도우 명령어
    그림판
    Solaris 10
    unix
    mspaint
    KakaoTalk
    vscode
    SFC
    Workspace
    스프링 프레임워크 핵심 기술
    알림
    dism
    제외
    MacOS
    윈도우
    logi options
    로지텍 마우스 제스처
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ 깊은 복사(Deep Copy)
상단으로

티스토리툴바