用静态成员做单列模式
#include<iostream>
using namespace std;
//单列模式:一个类只能创建一个对象
//
//class node
//{
// static node n;
// node() {}
// node(const node& n){}
//public:
// static node& getN()//不返回对象了,返回对象不让使用,返回引用才可以
// {
// return n;
// }
// void fun()
// {
// cout << "这是一个文字" << endl;
// }
//};
//
//node node::n;
//
//int main()
//{
// node& n1 = node::getN();//用引用的方式才可以
// node& n2 = node::getN();
//
// cout << &n1 << endl;//输出的这两个地址不一样
// cout << &n2 << endl;//输出的这两个地址不一样
//
// n1.fun();
// n2.fun();
// system("pause");
// return 0;
//}
class node
{
static node*n;
//构造和拷贝构造写在私有下面可以避免在外面创建新的对象
node(){}//函数构造
node(const node&n){} //拷贝构造
public:
static node* getN()
{
return n;
}
void fun()
{
cout << "aaaaa" << endl;
}
};
node* node::n = new node;//静态数据成员在类外进行初始化操作
int main()
{
node* p1 = node::getN();
node* p2 = node::getN();
cout << p1 << endl;
cout << p2 << endl;
p1->fun();
p2->fun();
system("pause");
return 0;
}