给你一段源代码, BC++ 3.1 下通过
#include <stdio.h>
class OneInstance
{
private:
static OneInstance* instance;
public:
void* operator new(size_t t = 1);
OneInstance();
int a;
};
OneInstance :: OneInstance(){
instance = this;
a = 1;
}
OneInstance * OneInstance :: instance = NULL;
void* OneInstance :: operator new(size_t t){
if(OneInstance::instance) return instance;
return ::new OneInstance [t];
}
int main()
{
OneInstance* p1,*p2;
p1 = new OneInstance;
p2 = new OneInstance;
p1->a++;
printf("%d", p2->a);
delete p1;
return 0;
}
跟踪进去发现 p1 和 p2 确实是指向同一个地址