C++單例模式

//單例模式
//程序運行時,該類只創建一次並可以調用類中的方法;
//不能手動創建
//必須得創建一次
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


class Stu{

static  Stu* s;
public:
    static Stu* func()
{
static Stu s;
printf("4helloworld\n");
return  &s;
}
void fun()
{
printf("5helloworld\n");
}
private:
Stu()
{
printf("1helloworld\n");
}
Stu(int a)
{
printf("2helloworld\n");
}
~Stu()
{
printf("3helloworld\n");
}
};
int main()
{
Stu::func();
Stu::func()->fun();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章