#include <iostream>
#include "boost/noncopyable.hpp"
template<class T>
class SingleInstance : public boost::noncopyable {
public:
static inline T instance() {
static T obj;
return obj;
}
};
class T {
public:
void show() {
std::cout << "show T...!" << std::endl;
}
};
#define G_T SingleInstance<T>::instance()
int main() {
G_T.show();
return 0;
}

本文展示了一种使用Boost库中的noncopyable类来实现C++单例模式的方法。通过模板类SingleInstance和宏G_T,确保了在整个程序中T类的唯一实例,并提供了实例的静态方法获取该实例。主要代码在main函数中调用了T类实例的show方法。
1572

被折叠的 条评论
为什么被折叠?



