C++ 在C++;
我已经基于hit构建了一个单例类 我使用C++ 在C++;,c++,singleton,C++,Singleton,我已经基于hit构建了一个单例类 我使用getMessage()函数对其进行了扩展,该函数将检索内部字典消息-字典只需在整个应用程序上加载一次,这就是单例的原因 我的代码: Singleton.hpp class Singleton { public: static Singleton& getInstance(); std::string getMessage(std::string code); private: Singleton() {};
getMessage()
函数对其进行了扩展,该函数将检索内部字典消息-字典只需在整个应用程序上加载一次,这就是单例的原因
我的代码:
Singleton.hpp
class Singleton {
public:
static Singleton& getInstance();
std::string getMessage(std::string code);
private:
Singleton() {};
Singleton(Singleton const&) = delete;
void operator=(Singleton const&) = delete;
};
Singleton& Singleton::getInstance()
{
static Singleton instance;
return instance;
}
std::string Singleton::getMessage(std::string code)
{
/// Do something
return "Code example.";
}
int main()
{
Singleton* my_singleton;
my_singleton = Singleton::getInstance(); **<-- ERROR HERE**
cout << my_singleton->getMessage("a"); << endl
}
Singleton.cpp
class Singleton {
public:
static Singleton& getInstance();
std::string getMessage(std::string code);
private:
Singleton() {};
Singleton(Singleton const&) = delete;
void operator=(Singleton const&) = delete;
};
Singleton& Singleton::getInstance()
{
static Singleton instance;
return instance;
}
std::string Singleton::getMessage(std::string code)
{
/// Do something
return "Code example.";
}
int main()
{
Singleton* my_singleton;
my_singleton = Singleton::getInstance(); **<-- ERROR HERE**
cout << my_singleton->getMessage("a"); << endl
}
以及主要代码:
main.cpp
class Singleton {
public:
static Singleton& getInstance();
std::string getMessage(std::string code);
private:
Singleton() {};
Singleton(Singleton const&) = delete;
void operator=(Singleton const&) = delete;
};
Singleton& Singleton::getInstance()
{
static Singleton instance;
return instance;
}
std::string Singleton::getMessage(std::string code)
{
/// Do something
return "Code example.";
}
int main()
{
Singleton* my_singleton;
my_singleton = Singleton::getInstance(); **<-- ERROR HERE**
cout << my_singleton->getMessage("a"); << endl
}
intmain()
{
单身人士*我的单身人士;
my_singleton=singleton::getInstance();***您希望存储对您的singleton的引用,而不是指针
Singleton& my_singleton = Singleton::getInstance();
您希望存储对单例的引用,而不是指针
Singleton& my_singleton = Singleton::getInstance();
那么,您可以这样调用函数:
Singleton::getInstance().getMessage("a");
而不是将其分配给变量。您只需像这样调用函数如何:
Singleton::getInstance().getMessage("a");
编译器生成的错误“singleton”声明为引用,但未初始化,以及其他一些关于singleton代码的错误…@Mendez您需要声明它并在上面的代码中使用相同的表达式进行初始化,就像在上面的代码中一样。我一直在我自己的代码中使用完全相同的singleton
,应该是这样的uld工作。但我更喜欢像@Christian一样访问它(并且使用点而不是->
)@Mendez请注意,我的答案在这个在线示例中确实有效:未工作。编译器生成的错误“singleton”声明为引用,但未初始化,以及其他一些关于singleton代码的错误…@Mendez您需要声明它并用与上述代码相同的表达式初始化,我在我自己的co中使用的是完全相同的singleton
de一直都应该有效。但我更喜欢像@Christian一样访问它(并且使用点而不是->
)@Mendez请注意,我的答案在这个在线示例中确实有效:不起作用,也没有意义,我无法将其存储在变量中…我需要修复如何将其存储在变量中,而不是避免它…实际上,这给了我一个如何解决它的提示…Singleton&my_Singleton=Singleton::getInstance();然后是my_Singleton.getMessage(“a”)@Mendez不需要引用:只需将其键入为:Singleton::getInstance().getMessage(“a”);
如果要缩短它,请使用宏:#定义Singleton Singleton::getInstance()
并将其与宏一起使用:theSingleton.getMessage(“a”)
不起作用,也没有意义,我不能将其存储在变量中…我需要修复如何将其存储在变量中,而不是避免它…实际上这给了我一个如何解决它的提示…Singleton&my_Singleton=Singleton::getInstance();然后是my_Singleton.getMessage(“a”)@Mendez不需要引用:只需将其键入:Singleton::getInstance().getMessage(“a”)
如果您想将其缩短,请使用宏:#定义Singleton Singleton::getInstance()
并将其与宏一起使用:theSingleton.getMessage(“a”)
您可能想看看这个,您可能想看看这个