static function -
A static function is a function whose scope is limited to the current source file.
static void function() {
cout << “function()";
}
static variable
in a function - 宣告初始化,只會呼叫一次
for(int i = 0; i < 10; i++) {
static int count = 0;
count++;
}
count值-->10
a class member - 此變數或function是屬於class的,不屬任何instance的,所以只會有一份,也因此沒有instance,就可以呼叫。
class myClass {
private:
static int id;
public:
static int next_id() {
id++;
return id;
}
};
int myClass::id = 0;
沒有留言:
張貼留言