dimanche 31 mars 2019

How to check that a static variable inside a class is null

I am new to programming and I was watching about Singleton design pattern.

I was trying to make a small implementation of the pattern by creating a Singleton class which have a static variable of type Singleton and a function that if the variable that i created is not instantiated to instantiate it otherwise to return that variable.


class Singleton
{
private:
    Singleton() { std::cout << "I am Singleton \n"; }
    static Singleton instance ;
public:
    static Singleton getInstance()
    {
        if (&instance == (Singleton*)nullptr)
        { 
        instance = *new Singleton;
        }

            return instance;
    }

};
int main()
{

    Singleton::getInstance();
    system ("pause");
} 

When i run it I get a error saying

error LNK2001: unresolved external symbol "private: static class Singleton Singleton::instance" (?instance@Singleton@@0V1@A)

Aucun commentaire:

Enregistrer un commentaire