C++ Singleton
來源:程序員人生 發布時間:2014-10-06 08:00:00 閱讀次數:2413次
Singleton in C++:
#include <iostream>
using namespace std;
class Restaurant {
public:
static Restaurant *getInstance() {
if (instance == NULL) instance = new Restaurant(100);
return instance;
}
int getSize() { return size; }
void setSize(int _size) { size = _size; }
private:
Restaurant(int _size) { size = _size; }
int size;
static Restaurant *instance;
};
Restaurant *Restaurant::instance = NULL;
int main() {
cout<<Restaurant::getInstance()->getSize();
return 0;
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈