C++ Singleton

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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章