網頁

2017年10月29日 星期日

20171019

static在C++的種類有五種使用方式

1. Static variable in a file
2. Static variable in a function
3. Static function
4. Static data member of a class 5. Static member function of a class
static在三種scope下有不同功能
1. global
global variable宣告static會讓global scope變file scope,意思是說當兩份cpp檔如果都有相同變數名稱時,他們各自屬於他們file內的變數,彼此之間沒有關係。
2. function
在function裡,local variable宣告static時,不會因為function被回收讓變數也被回收。
3. class
static data member
(i)不管產生多少個相同class的物件,static的members只會複製一份,而且共享給每一個物件。
(ii)不必在class scope內初始化。利用::在class外部初始化,如果沒有,系統會自動初始化為0。
static member function
只能用::access,即使沒有產生物件。
只能access “static” data member,enum, or nested types。
不能access到this pointer

沒有留言:

張貼留言