C++ 정적 멤버 변수 (Static Member Variable)

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

정적 멤버 변수 (Static Member Variable)

  • 클래스 내부에 static 변수를 정의할 때는 직접 초기화할 수 없다.

    • 중복 선언 문제인 것 같다.

    • 생성자에서도 초기화할 수 없다.

      • Inner Class를 사용해서 우회하여 초기화할 수는 있다.

예제

  • static이 아닌 기본 예제

    #include <iostream>
    
    class Something
    {
    public:
      int    value_ = 1;
    };
    
    int        main()
    {
      using namespace std;
    
      Something st1;
      Something st2;
    
      st1.value_ = 2;
    
      cout << &st1.value_ << ' ' << st1.value_ << endl;
      cout << &st2.value_ << ' ' << st2.value_ << endl;
    }
    
    /* stdout
    00CFFB4C 2
    00CFFB40 1
    */
  • static 예제

    #include <iostream>
    
    class Something
    {
    public:
      static int    s_value_;
    };
    
    int Something::s_value_ = 1;  // define in .cpp file(not in header file)
    
    int        main()
    {
      using namespace std;
    
      cout << &Something::s_value_ << ' ' << Something::s_value_ << endl;
    
      Something st1;
      Something st2;
    
      st1.s_value_ = 2;
    
      cout << &st1.s_value_ << ' ' << st1.s_value_ << endl;
      cout << &st2.s_value_ << ' ' << st2.s_value_ << endl;
    
      Something::s_value_ = 1024;
    
      cout << &Something::s_value_ << ' ' << Something::s_value_ << endl;
    }
    
    /* stdout
    0032C008 1
    0032C008 2
    0032C008 2
    0032C008 1024
    */
    • 클래스 외부에서 정의해야 한다는 점이 좀 헷갈린다.

Inner Class

  • Inner Class를 사용한 static 변수의 클래스 내부 초기화

    #include <iostream>
    
    class Something
    {
    public:
      class _init
      {
      public:
        _init()
        {
          s_value_ = 9876;
        }
      };
    
    private:
      static int        s_value_;
      static _init      s_initializer;
    
    public:
      static int    getValue()
      {
        return s_value_;
      }
    };
    
    int                    Something::s_value_;
    Something::_init    Something::s_initializer;
    
    int        main()
    {
      using namespace std;
    
      Something    s1;
    
      cout << s1.getValue() << endl;
    }
    
    /* stdout
    9876
    */
저작자표시

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

C++ friend  (0) 2021.03.16
C++ 정적 멤버 함수 (Static Member Function)  (0) 2021.03.16
C++ static (Class)  (0) 2021.03.15
C++ 체이닝(Chaining)  (0) 2021.03.15
C++ this  (0) 2021.03.15
'C++/Class' 카테고리의 다른 글
  • C++ friend
  • C++ 정적 멤버 함수 (Static Member Function)
  • C++ static (Class)
  • C++ 체이닝(Chaining)
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 분류 전체보기 (316)
      • 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 (19)
        • Registry (3)
        • WSL (1)
        • DeviceDriver (6)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ 정적 멤버 변수 (Static Member Variable)
상단으로

티스토리툴바