Singleton设计模式
思路如以下:
Single.h
#pragma once #include <iostream> #include <string> using namespace std; class Single { public: static Single& Instance(void) { static Single s_single; return s_single; } void Print() const; private: CSingleton(void){} CSingleton(const CSingleton& oth); };
Single.cpp
#include "single.h" void CSingleton::Print()const { cout << "Single!" << endl; }
main.cpp
#include "single.h" int main() { CSingleton::Instance().Print(); return 0; }
执行后打印Single!
版权声明:本文博客原创文章。博客,未经同意,不得转载。