C语言单例实例

2017-01-13 08:17:28来源:CSDN作者:qq_27396861人点击

1) 懒汉模式 
User.h
class User {public:    static User* instance();
private:    User();    static User* _user;};


User.cpp
User::User() { }
User* User::_user = NULL;
User* User::instance() {    if (_user == NULL)        _user = new User;    return _user;}
2)饿汉模式
User.h
class User {public:    static User* instance();
private:    User();    static User* _user;};
User.cpp
User::User() { }
User* User::_user = new User;
User* User::instance() {    return _user;}
3)
User.h
class User {public:    static User* instance();
private:    User();};
User.cpp
User::User() { }
User* User::instance() {    static User* user = new User;    return user;}

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台