C++单态模式的问题
这个程序没有问题,只是有个疑问:为什么每次都调用new A后, 返回的地址都是一样的呢?
#include <iostream>
using namespace std;
class A
{
public:
static A& instance()
{
static A& inst = *(new A);
cout<<&inst<<endl; //inst的地址一直是一样的
return inst;
}
void incre()
{
count++;
cout<<count<<endl;
}
private:
A(){count = 0;}
int count;
};
void ShowCount()
{
A::instance().incre();
}
int main(int argc, char* argv[])
{
for (int i = 0; i < 1000; i++)
{
ShowCount();
}
return 0;
}