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

2021. 3. 15. 23:59·SW개발/C++
반응형

정적 멤버 변수 (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
    */
반응형
저작자표시 (새창열림)

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

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
'SW개발/C++' 카테고리의 다른 글
  • C++ friend
  • C++ 정적 멤버 함수 (Static Member Function)
  • C++ static (Class)
  • C++ 체이닝(Chaining)
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 전체보기 (318)
      • SW개발 (268)
        • Java Spring (6)
        • C++ (186)
        • Python (21)
        • Linux (16)
        • 알고리즘 (13)
        • Git (4)
        • Embedded (1)
        • Raspberrypi (9)
        • React (3)
        • Web (2)
        • Windows Device Driver (6)
      • IT(개발아님) (45)
        • Windows (25)
        • MacOS (7)
        • Utility (11)
      • 챗봇 짬통 (0)
      • 일상 (2)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

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

티스토리툴바