C++单例模式

2022/2/2 12:43:16

本文主要是介绍C++单例模式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

单例模式

#include <iostream>
using namespace std;

class Person
{
    private:
        string id;
        string hometown;
        static Person *ptr;
    public:
        static Person* create()
        {
            if(ptr == NULL)
            {
                ptr = new Person;
            }
            return ptr;
        }
};

Person* Person::ptr = NULL;

int main()
{
    Person *p = Person::create();
    system("pause");
    return 0;
}


这篇关于C++单例模式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程