cpp에서는 class variable을 쓸 때,
어떤 cpp 파일에
12 |
int Something::s_nIDGenerator = 1; |
01 |
class Something |
02 |
{ |
03 |
private : |
04 |
static int s_nIDGenerator; |
05 |
int m_nID; |
06 |
public : |
07 |
Something() { m_nID = s_nIDGenerator++; } |
08 |
09 |
int GetID() const { return m_nID; } |
10 |
}; |
11 |
12 |
int Something::s_nIDGenerator = 1; |
13 |
14 |
int main() |
15 |
{ |
16 |
Something cFirst; |
17 |
Something cSecond; |
18 |
Something cThird; |
19 |
20 |
using namespace std; |
21 |
cout << cFirst.GetID() << endl; |
22 |
cout << cSecond.GetID() << endl; |
23 |
cout << cThird.GetID() << endl; |
24 |
return 0; |
25 |
} |
http://www.learncpp.com/cpp-tutorial/811-static-member-variables/