我想了解会发生什么,如果我们分配静态函数内的动态内存?每次调用静态函数都会返回相同的内存,或者每次创建新内存?动态内存分配里面的静态函数
对于示例 -
class sample
{
private:
static int *p;
public:
static int* allocate_memory()
{
p = new int();
return p;
}
};
int* sample::p=NULL;
int main()
{
int *p= sample::allocate_memory();
int *q = sample::allocate_memory();
cout << p << " " << q << endl;
if (p== q)
cout << "we are equal";
}
在该程序中主两个存储器位置()是不同的。如果我们移动static int * p;在allocate_memory()函数内部,像static int * p = new int;两个内存位置都会一样。
我想了解有什么不同。静态总是静态的,天气是内部类还是内部函数,那为什么行为不同?
Devesh