Re: [問題] 對static的一點疑問
看板C_and_CPP (C/C++)作者darklady (Sylvanas)時間16年前 (2009/01/29 22:05)推噓5(5推 0噓 11→)留言16則, 8人參與討論串2/2 (看更多)
我又更改程式做一點測試但是結果更讓我疑惑了
1 #include <iostream>
2 using namespace std;
3
4 size_t count_calls()
5 {
6 static size_t ctr = 0;
7 return ++ctr;
8 }
9
10 size_t test_static()
11 {
12 static size_t ctr = 100;
13 return ctr;
14 }
15
16 int main()
17 {
18
19 for (size_t i = 0; i != 10; ++i)
20 cout << count_calls() << endl;
21 cout << test_static() << endl;
22
23 }
跑出的結果是
1
2
3
4
5
6
7
8
9
10
100
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.161.222.220
※ 編輯: darklady 來自: 118.161.222.220 (01/29 22:06)
→
01/29 22:13, , 1F
01/29 22:13, 1F
推
01/29 22:14, , 2F
01/29 22:14, 2F
推
01/29 22:16, , 3F
01/29 22:16, 3F
推
01/29 22:42, , 4F
01/29 22:42, 4F
→
01/30 00:05, , 5F
01/30 00:05, 5F
→
01/30 00:05, , 6F
01/30 00:05, 6F
→
01/30 00:08, , 7F
01/30 00:08, 7F
→
01/30 00:09, , 8F
01/30 00:09, 8F
→
01/30 00:10, , 9F
01/30 00:10, 9F
→
01/30 00:11, , 10F
01/30 00:11, 10F
→
01/30 00:14, , 11F
01/30 00:14, 11F
→
01/30 00:17, , 12F
01/30 00:17, 12F
→
01/30 00:17, , 13F
01/30 00:17, 13F
→
01/30 00:18, , 14F
01/30 00:18, 14F
推
01/30 14:02, , 15F
01/30 14:02, 15F
推
01/31 00:21, , 16F
01/31 00:21, 16F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章