[C++] static
Updated:
static 멤버 함수/변수
- 프로그램 실행 시 생성되어 종료 시 소멸
코드
#include <iostream>
using namespace std;
class Test {
private:
static int I;
public:
Test() = default;
~Test() = default;
static void AddI(int i) { Test::I += i; }
static void Show() { cout << Test::I << endl; }
int GetI() { return Test::I; }
};
int Test::I = 0;
int main() {
Test::AddI(1);
Test::Show();
Test t;
t.AddI(1);
cout << t.GetI() << endl;
return 0;
}
실행 결과
1
2