C++ 형변환 오버로딩 (Typecasts Overloading)

2021. 3. 16. 00:29·C++/Overloading

형변환 오버로딩 (Typecasts Overloading)

  • (int)객체, int(객체), static_cast<int>(객체) 등의 형태로 형변환을 할 수 있게 해준다.

  • 묵시적 형변환도 가능해진다.


예제

  #include <iostream>

  class Cents
  {
    int cents_;

  public:
    Cents(int cents = 0) { cents_ = cents; }

    operator int()
    {
      std::cout << "cast here" << std::endl;
      return cents_;
    }
  };

  void    printInt(const int& value)
  {
    std::cout << value << std::endl;
  }

  int        main()
  {
    using namespace std;

    Cents    cents(7);

    printInt(cents);

    int value;

    value = (int)cents;
    value = int(cents);
    value = static_cast<int>(cents);
  }

  /* stdout
  cast here
  7
  cast here
  cast here
  cast here
  */
  • 다른 객체로의 형변환 오버로딩도 가능하다.

    #include <iostream>
    
    class Cents
    {
      int cents_ = 0;
    
    public:
      Cents(const int& cents = 0) { cents_ = cents; }
    
      operator int()
      {
        std::cout << "cast here" << std::endl;
        return cents_;
      }
    };
    
    class Dollars
    {
      int    dollars_ = 0;
    
    public:
      Dollars(const int& dollars = 0) { dollars_ = dollars; }
    
      operator Cents()
      {
        return Cents(dollars_ * 100);
      }
    };
    
    void    printInt(const int& value)
    {
      std::cout << value << std::endl;
    }
    
    int        main()
    {
      using namespace std;
    
      Dollars    dol(2);
      Cents    cents(dol);
    
      printInt(cents);
    }
    
    /* stdout
    cast here
    200
    */
저작자표시 (새창열림)

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

C++ 대입 연산자 오버로딩 (Assignment Operator Overloading)  (0) 2021.03.19
C++ 괄호 연산자 오버로딩 (Parenthesis Operator Overloading)  (0) 2021.03.16
C++ 첨자 연산자 오버로딩 (Subscript Operator Overloading)  (0) 2021.03.16
C++ 증감 연산자 오버로딩 (Increment and Decrement Operator Overloading)  (0) 2021.03.16
C++ 비교 연산자 오버로딩 (Comparison Operator Overloading)  (0) 2021.03.16
'C++/Overloading' 카테고리의 다른 글
  • C++ 대입 연산자 오버로딩 (Assignment Operator Overloading)
  • C++ 괄호 연산자 오버로딩 (Parenthesis Operator Overloading)
  • C++ 첨자 연산자 오버로딩 (Subscript Operator Overloading)
  • C++ 증감 연산자 오버로딩 (Increment and Decrement Operator Overloading)
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)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ 형변환 오버로딩 (Typecasts Overloading)
상단으로

티스토리툴바