c 2b 2b pass argument to singleton

Solutions on MaxInterview for c 2b 2b pass argument to singleton by the best coders in the world

showing results for - "c 2b 2b pass argument to singleton"
Yoan
08 Jan 2020
1class Questionnary
2{
3  std::string _str;
4
5  static Questionnary& getInstanceImpl(std::string* const s = nullptr)
6  {
7    static Questionnary instance{ s };
8    return instance;
9  }
10
11  Questionnary(std::string* const s)
12    : _str{ s ? move(*s) : std::string{} } // employ move ctor
13  {
14    if (nullptr == s)
15      throw std::runtime_error{ "Questionnary not initialized" };
16  }
17
18public:
19  static Questionnary& getInstance()
20  {
21    return getInstanceImpl();
22  }
23  static void init(std::string s) // enable moving in
24  {
25    getInstanceImpl(&s);
26  }
27
28  Questionnary(Questionnary const&) = delete;
29  void operator=(Questionnary const&) = delete;
30};
31