在Cocos2Dx中实现Singleton的正确方法
Correct way to implement Singleton in Cocos2Dx
目前我使用的方法是:
class Singleton {
public:
static Singleton &getInstance() {
static Singleton *instance = new Singleton();
return *instance;
}
void getData();
private:
Singleton() {}
};
通过这种方式,我可以使用Singleton写作中的一种方法:
Singleton::getInstance.getData();
这似乎是阅读大量C++11教程的正确方式。但是通过阅读cocos-Director的单例代码(还有FileUtils等),我发现cocos使用了另一种方法:
class Singleton {
public:
static Singleton *getInstance() {
instance = new Singleton();
return instance;
}
void getData();
private:
Singleton() {}
static Singleton *instance;
};
用这种方法我必须写:
Singleton::getInstance->getData();
因为指针*getInstance而不是引用&getInstance。
我认为差异很大,但我不知道一种方法是正确的,另一种方法不是。
请帮我理清这个概念。
在我看来,最好的单例是以值类型的形式呈现的,带有隐藏的单例实现。
这意味着您可以像传递任何其他对象一样传递singleton。
这反过来意味着,如果您稍后改变主意,并且singleton实际上需要成为一个普通对象,则不需要更改任何代码。
这也意味着您的singleton对象可以参与ADL和标记调度。
例如:
#include <iostream>
// only this goes in the header file
struct my_thing
{
// public interface
int do_or_get_something(int arg);
private:
struct impl;
static impl& get_impl();
};
// this goes in the cpp file
struct my_thing::impl
{
int do_or_get_something(int arg)
{
// for demo purposes
return counter += arg;
}
int counter = 0; // for demo purposes
};
my_thing::impl& my_thing::get_impl()
{
// create first time code flows over it - c++ standard
// thread safe in c++11 - guarantee
static impl instance {};
return instance;
}
// implement public interface in terms of private impl
int my_thing::do_or_get_something(int arg)
{
return get_impl().do_or_get_something(arg);
}
// test the concept
int main()
{
auto one_thing = my_thing();
std::cout << one_thing.do_or_get_something(5) << std::endl;
std::cout << one_thing.do_or_get_something(5) << std::endl;
auto another_thing_but_really_same = my_thing();
std::cout << another_thing_but_really_same.do_or_get_something(5) << std::endl;
std::cout << my_thing().do_or_get_something(5) << std::endl;
std::cout << one_thing.do_or_get_something(5) << std::endl;
}
预期输出:
5
10
15
20
25
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 通过方法访问结构
- 最小硬币更换问题(自上而下方法)
- C++为构建时间获取QDateTime的可靠方法
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 如何将Singleton类信号连接到MainWindow类插槽或方法
- 在用户类中包括PYBIND11嵌入式模块的正确方法(作为Singleton类的一部分)是什么?
- Singleton实现:与Singleton类相比,名称空间方法通常更可取吗
- 具有模板化类型的动态类型Singleton.这是可行的方法吗?[提供的解决方案]
- 在其方法中获取singleton实例
- 在Cocos2Dx中实现Singleton的正确方法
- Singleton类贯穿整个项目的方法
- 如何在 c++ 中的 Normal 或 Singleton 类中 GoogleTest 私有方法/枚举类
- 构造函数的Singleton方法
- 作为静态字段的Singleton实例与getInstance()方法中的静态变量
- 访问blackberry 10中的singleton类方法
- 如何在不使用非构造函数的方法的情况下应用singleton设计模式来返回类对象
- 使用Singleton类的最有效方法
- 静态方法与singleton
- c++:logger类,不带全局变量或singleton,或将其传递给每个方法