C++ 변환 생성자(Converting Constructor)

2021. 3. 15. 23:32·C++/Class

변환 생성자(Converting Constructor)

  • explicit 키워드가 없는 생성자는 묵시적으로 형변환을 허용한다.

  • 변환이 가능한 경우는 해당 인자를 생성자에 인자로 주었을 때 유효한 경우이다.

    #include <iostream>
    #include <cassert>
    
    class Fraction
    {
      int    numerator_;
      int    denominator_;
    
    public:
      Fraction(int num = 0, int den = 1)
        : numerator_(num), denominator_(den)
      {
        std::cout << "Default constructor" << std::endl;
        assert(den != 0);
      }
    
      Fraction(const Fraction& fraction)
        : numerator_(fraction.numerator_), denominator_(fraction.denominator_)
      {
        std::cout << "Copy constructor called" << std::endl;
      }
    
      friend std::ostream& operator << (std::ostream& out, const Fraction& f)
      {
        out << f.numerator_ << " / " << f.denominator_;
        return (out);
      }
    };
    
    void    doSomething(Fraction frac)
    {
      std::cout << frac << std::endl;
    }
    
    int        main()
    {
      using namespace std;
    
      doSomething(7);
    }
    
    /* stdout
    Default constructor
    7 / 1
    */
    • 위의 기본 생성자 앞에 explicit 키워드를 붙이면, 묵시적 형변환을 금지시키는 것이 되어 에러가 출력된다.

      error C2664: 'void doSomething(Fraction)': cannot convert argument 1 from 'int' to 'Fraction'
저작자표시 (새창열림)

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

C++ this  (0) 2021.03.15
C++ 소멸자 (Destructor)  (0) 2021.03.15
C++ 복사 생성자(Copy Constructor)  (0) 2021.03.12
C++ 위임 생성자 (Delegating Constructor)  (0) 2021.03.12
C++ 멤버 초기화 리스트 (Member Initializer Lists)  (0) 2021.03.12
'C++/Class' 카테고리의 다른 글
  • C++ this
  • C++ 소멸자 (Destructor)
  • C++ 복사 생성자(Copy Constructor)
  • C++ 위임 생성자 (Delegating Constructor)
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)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ 변환 생성자(Converting Constructor)
상단으로

티스토리툴바