关于C++单例模式的一点小疑问。。。
#include "stdafx.h"
#include <iostream>
using namespace std;
class A
{
public:
static A* instance()
{
static A _a;
return &_a;
}
void setNum(int num)
{
this->num = num;
}
void getNum()
{
cout<<"num:"<<this->num<<endl;
//return this.num;
}
private:
A();
private:
int num;
};
A::A(){};
class B
{
public:
static B* instance()
{
return b;
}
void setNum(int num)
{
this->num = num;
}
void getNum()
{
cout<<"num:"<<this->num<<endl;
//return this.num;
}
private:
B();
static B *b;
int num;
};
B::B(){};
B* B::b = new B();
class C
{
public:
static C* instance()
{
if (c==NULL)
{
c = new C();
}
return c;
}
void setNum(int num)
{
this->num = num;
}
void getNum()
{
cout<<"num:"<<this->num<<endl;
//return this.num;
}
private:
C();
static C *c;
int num;
};
C::C(){};
C* C::c = NULL;
int main( )
{
A::instance()->setNum(15);
A::instance()->getNum();
B::instance()->setNum(16);
B::instance()->getNum();
C::instance()->setNum(17);
C::instance()->getNum();
system("pause");
return 0;
}