250x250
Notice
Recent Posts
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
관리 메뉴

일상 코딩

[C++/8.10] 클래스와 static 변수 variable 본문

C++/따배C++ 08강 객체지향 기초

[C++/8.10] 클래스와 static 변수 variable

polarcompass 2021. 10. 23. 08:28
728x90

static 변수는 cpp 파일에서만 선언한다.

#include<iostream>
using namespace std;

class Something
{
public:
    static int s_value;
};

int Something::s_value = 1; // define in cpp file(static 값일 경우.)

int main()
{
    cout << &Something::s_value << " -> " << Something::s_value << endl;
    
    for(int i=0; i<20; i++)
        cout << "-";
    cout << 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;

    for(int i=0; i<20; i++)
        cout << "-";
    cout << endl;

    Something::s_value = 1024;
    cout << &Something::s_value << " -> " << Something::s_value << endl;

    return 0;
}

터미널 출력

터미널 출력 화면

 

728x90