Correct way to call a singleton object in C++ -
i've build singleton class based on hit posted here.
i've extended getmessage()
function, retrive internal dictionary message - dictionary needs loaded once on whole application, reason of singleton.
my code:
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.cpp
singleton& singleton::getinstance() { static singleton instance; return instance; } std::string singleton::getmessage(std::string code) { /// return "code example."; }
and main code:
main.cpp
int main() { singleton* my_singleton; my_singleton = singleton::getinstance(); **<-- error here** cout << my_singleton->getmessage("a"); << endl }
main giving me error: cannot convert 'singleton' 'singleton*' in assignment
what correct way "instantiate" singleton , make use of getmessage function.
thanks lot helping...
what call function this:
singleton::getinstance().getmessage("a");
instead of assigning variable.