C++ 에서 class 멤버 중 static 으로 선언된 멤버는 그 class로 생성된 객체(인스턴스)가 몇개든 간에 한개만을 메모리 할당을 하여 공유하여 사용한다.
C++ 에서 class( 멤버 변수, 멤버 함수의 선언 & 정의 ) 에 static 이 사용되면 그 class 로 생성된 객체(인스턴스)가 몇개든 간에 한개만을 메모리 할당을 공유하여 사용한다.
Example
The following example shows the use of static in a class.
// static2.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
class CMyClass {
public:
static int m_i;
};
int CMyClass::m_i = 0;
int main() {
CMyClass a,b;
cout << a.m_i << endl;
cout << b.m_i << endl;
a.m_i = 1;
cout << a.m_i << endl;
cout << b.m_i << endl;
}
Output
0
0
1
1
객체(Object)간의 공유를 위해 많이 사용된다.
'Programming > C++' 카테고리의 다른 글
Effective C++ 정리글(펌) (0) | 2009.11.21 |
---|---|
비트 연산자 (0) | 2009.11.21 |
namespace Declaration (0) | 2009.11.20 |
friend 함수 (0) | 2009.11.20 |
C++ inline 함수 (0) | 2009.11.19 |