samedi 4 avril 2020

Creating a singleton class in c++

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?

Aucun commentaire:

Enregistrer un commentaire