0

Firstly, I am new to C++ and Stackoverflow community. I am trying to learn singleton class. As per the answer given in the post https://stackoverflow.com/a/15310943/7023011, i tried using the same code but just defined the static object as a static data member outside the member function as shown:

struct Example
{
    static Example example;
    static Example& instance()
    {
        return example;
    }

 private:

    Example() { }
    Example(Example const&) = delete;
    Example(Example&&) = delete;
    Example& operator = (Example const&) = delete;
    Example& operator = (Example&&) = delete;

};

But this process gives compilation error: "undefined reference to Example::example" . Can anyone suggest the difference between the two?

  • You've only provided a declaration of `example`. You also need a definition. In one source file, add `Example Example::example;` That said, it's best to make it a static local variable inside `instance()` function, like in the answer you cite. – Igor Tandetnik Apr 05 '20 at 00:48
  • The code works if i remove the reference operator "&" from the line "static Example& instance()". I don't understand why. Even though i don't provide the definition. Any suggestion? Also why is my method not preferred as mentioned by @Igor Tandetnik. – blue_beetle Apr 05 '20 at 01:30
  • No, [the code does not work](https://rextester.com/NCC21846) if you remove the ampersand. – Igor Tandetnik Apr 05 '20 at 01:54
  • Excuse me if my questions are silly, but what if i only keep default constructor in the private access specifier and remove other lines of code(from 12 to 15). Then it works.Why so ? @IgorTandetnik – blue_beetle Apr 05 '20 at 02:15
  • But then it's not a singleton anymore. `instance()` returns a copy of `example`; you are creating multiple instances of `Example` class. – Igor Tandetnik Apr 05 '20 at 02:17
  • Okay i researched and got the concept in your last comment, as we are preventing the use of copy constructor. So finally i am left with the doubt why are we using the & sign there, even tough we are return an object of type Example. @IgorTandetnik – blue_beetle Apr 05 '20 at 03:25
  • If the return type is `Example&`, then `instance()` is not returning an object; it's returning a reference. – Igor Tandetnik Apr 05 '20 at 03:35
  • Thanks a lot for helping me till here. Just one last fix. As you mentioned about the function definition in the first comment in this thread. Why is it that in the implementation link i mentioned in the question, the author has not mentioned the definition. Is it self understood or is it not required in his example. ? @IgorTandetnik – blue_beetle Apr 05 '20 at 04:58
  • The answer you cite doesn't use a static data member, but a static local variable (it's defined in the body of the function, not that of the class). For such a variable, `static Example example;` is both a declaration and a definition. – Igor Tandetnik Apr 05 '20 at 12:13
  • Thanks a tonne. i got many concepts cleared through this conversation..!! @IgorTandetnik – blue_beetle Apr 06 '20 at 05:54

0 Answers0