C++ 첨자 연산자 오버로딩 (Subscript Operator Overloading)

2021. 3. 16. 00:26·SW개발/C++
반응형

첨자 연산자 오버로딩 (Subscript Operator Overloading)

  • [](subscript operator) 내부에 숫자 뿐만 아니라 문자 등 다양한 자료형이 들어갈 수 있다.

예제

  #include <iostream>

  class IntList
  {
    int    list_[10];

  public:
    int& operator [] (const int index)
    {
      return list_[index];
    }
  };

  int        main()
  {
    using namespace std;

    IntList my_list;
    my_list[3] = 1;
    cout << my_list[3] << endl;
    my_list[3] = 10;
    cout << my_list[3] << endl;
  }

  /* stdout
  1
  10
  */
  • const를 사용할 경우

    #include <iostream>
    
    class IntList
    {
      int    list_[10] = { 0, 1, 2, 3, 4, 5 };
    
    public:
      int& operator [] (const int index)
      {
        return list_[index];
      }
    
      const int& operator [] (const int index) const
      {
        return list_[index];
      }
    };
    
    int        main()
    {
      using namespace std;
    
      const IntList my_list;
    
      cout << my_list[3] << endl;
    }
    
    /* stdout
    3
    */
반응형
저작자표시 (새창열림)

'SW개발 > C++' 카테고리의 다른 글

C++ 형변환 오버로딩 (Typecasts Overloading)  (0) 2021.03.16
C++ 괄호 연산자 오버로딩 (Parenthesis 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++ 단항 연산자 오버로딩 (Unary Operator Overloading)  (0) 2021.03.16
'SW개발/C++' 카테고리의 다른 글
  • C++ 형변환 오버로딩 (Typecasts Overloading)
  • C++ 괄호 연산자 오버로딩 (Parenthesis Operator Overloading)
  • C++ 증감 연산자 오버로딩 (Increment and Decrement Operator Overloading)
  • C++ 비교 연산자 오버로딩 (Comparison Operator Overloading)
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 전체보기 (321) N
      • SW개발 (269) N
        • Java Spring (6)
        • C++ (186)
        • Python (21)
        • Linux (16)
        • 알고리즘 (13)
        • Git (4)
        • Embedded (1)
        • Raspberrypi (9)
        • React (3)
        • Web (3) N
        • Windows Device Driver (6)
      • IT(개발아님) (47)
        • Windows (26)
        • MacOS (8)
        • Utility (11)
      • 챗봇 짬통 (0)
      • 일상 (2)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ 첨자 연산자 오버로딩 (Subscript Operator Overloading)
상단으로

티스토리툴바