C++笔记之单例模式
参考笔记:C++笔记之call_once和once_flag
code review
文章目录
1.返回实例引用
代码
cpp
#include <iostream>
class Singleton {
public:
static Singleton &getInstance() {
static Singleton instance;
return instance;
}
void printMessage() {
std::cout << "hello world!" << std::endl;
}
private:
Singleton() {} // 禁止外部创建实例
Singleton(const Singleton &); // 禁止复制实例
Singleton operator=(const Singleton &); // 禁止赋值实例
};
int main() {
Singleton &instance = Singleton::getInstance();
instance.printMessage();
return 0;
}
2.返回实例指针
代码
cpp
#include <iostream>
class Singleton {
public:
static Singleton *getInstance() {
if (instance == nullptr) {
instance = new Singleton();
}
return instance;
}
void printMessage() {
std::cout << "hello world!" << std::endl;
}
private:
static Singleton *instance;
Singleton() {} // 禁止外部创建实例
Singleton(const Singleton &); // 禁止复制实例
Singleton operator=(const Singleton &); // 禁止赋值实例
};
Singleton *Singleton::instance = nullptr;
int main() {
Singleton *instance = Singleton::getInstance();
instance->printMessage();
return 0;
}
3.单例和智能指针share_ptr结合
代码
cpp
#include <iostream>
#include <memory>
class Singleton {
public:
static std::shared_ptr<Singleton> getInstance() {
if (!instance) {
instance = std::shared_ptr<Singleton>(new Singleton());
}
return instance;
}
void printMessage() {
std::cout << "hello world!" << std::endl;
}
private:
Singleton() {} // 禁止外部创建实例
Singleton(const Singleton &); // 禁止复制实例
Singleton operator=(const Singleton &); // 禁止赋值实例
static std::shared_ptr<Singleton> instance;
};
std::shared_ptr<Singleton> Singleton::instance = nullptr;
int main() {
std::shared_ptr<Singleton> instance = Singleton::getInstance();
instance->printMessage();
return 0;
}
4.单例和std::call_once结合
编译运行:
代码
cpp
#include <iostream>
#include <mutex>
class Singleton {
public:
static Singleton *getInstance() {
std::call_once(initFlag, [](){
instance = new Singleton();
});
return instance;
}
void printMessage() {
std::cout << "hello world!" << std::endl;
}
private:
static Singleton *instance;
static std::once_flag initFlag;
Singleton() {} // 禁止外部创建实例
Singleton(const Singleton &); // 禁止复制实例
Singleton operator=(const Singleton &); // 禁止赋值实例
};
Singleton *Singleton::instance = nullptr;
std::once_flag Singleton::initFlag;
int main() {
Singleton *instance = Singleton::getInstance();
instance->printMessage();
return 0;
}
5.单例和std::call_once、unique_ptr结合
编译运行
代码
cpp
#include <iostream>
#include <mutex>
#include <memory>
class Singleton {
public:
static Singleton *getInstance() {
std::call_once(initFlag, [](){
instance.reset(new Singleton());
});
return instance.get();
}
void printMessage() {
std::cout << "hello world!" << std::endl;
}
private:
static std::unique_ptr<Singleton> instance;
static std::once_flag initFlag;
Singleton() {} // 禁止外部创建实例
Singleton(const Singleton &); // 禁止复制实例
Singleton operator=(const Singleton &); // 禁止赋值实例
};
std::unique_ptr<Singleton> Singleton::instance;
std::once_flag Singleton::initFlag;
int main() {
Singleton *instance = Singleton::getInstance();
instance->printMessage();
return 0;
}