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