6
在這種情況下,應該只有一個或零個靜態變量實例。這取決於f()
是否已被調用。靜態變量的有多少個實例在方法中聲明的靜態變量?
void f()
{
static int a;
}
但是有多少實例是那裏,如果f()
是一種方法?
class A
{
void f()
{
static int a;
}
};
在這種情況下,應該只有一個或零個靜態變量實例。這取決於f()
是否已被調用。靜態變量的有多少個實例在方法中聲明的靜態變量?
void f()
{
static int a;
}
但是有多少實例是那裏,如果f()
是一種方法?
class A
{
void f()
{
static int a;
}
};
一樣的功能:0或1。這是很容易查得:
class A
{
public:
void f()
{
static int a = 0;
++a;
cout << a << endl;
}
};
int main()
{
A a;
a.f();
a.f();
A b;
b.f();
}
輸出:
1
2
3
但是,如果從class A
derieve,使虛擬功能像這樣:
class A
{
public:
virtual void f()
{
static int a = 0;
++a;
cout << a << endl;
}
};
class B:public A
{
public:
void f()
{
static int a = 0;
++a;
cout << a << endl;
}
};
那麼a
變量對於基數和每個派生類都是不同的(因爲函數也不同)。
相同...作爲成員函數是正交於靜態本地。
+ 1 for'orthogonal' – steffen 2012-07-06 06:42:15