0

I was trying to understand singleton design pattern and created a simplest one:

#include <iostream>


class mySingleton{

private:
   static mySingleton *ptr;
   mySingleton(){ }    

public:
   static mySingleton* getInstance(){
     if(!ptr){
        ptr = new mySingleton();
        return ptr;
     } else return ptr;
   }

   void msg(){
     std::cout << " Hello World!! " << std::endl;
   }

};


int main(){

mySingleton* obj = mySingleton::getInstance();
mySingleton* obj2 = mySingleton::getInstance();

return 0;
}

When I try to compile I get :

Undefined symbols for architecture x86_64:
"mySingleton::ptr", referenced from:
    mySingleton::getInstance()       in ccm822LI.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

Why can't I use ptr inside a static function, since ptr is also a static variable? Am I missing something here?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
pokche
  • 1,141
  • 12
  • 36

2 Answers2

7

Am I missing something here?

Yes, several things:

  1. As mentioned you are missing the definition of the static mySingleton pointer variable.
  2. Your code isn't thread safe
    The correct way to implement it is to use a local static variable in the getInstance() function (aka. Scott Meyer's Singleton):

    static mySingleton* getInstance(){
        static mySingleton theInstance;
        return &theinstance;
    }
    

    This implementation is guaranteed to be thread safe, and you don't need to bother with memory allocation.

  3. Using a pointer probably isn't what you want as a return type

    static mySingleton& getInstance(){
                   // ^
        static mySingleton theInstance;
        return theinstance;
    }
    
Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Thank you for the answer .. one last thing I want to wrap my head around is .. so getInstance() can be called multiple times ... but how come the object it instantiate is always the same .. I know its because of static but I am still missing some thing that I need to understand – pokche Jun 23 '16 at 18:08
  • @pokche The 1st call to `getInstance()` triggers to create the instance, any following will get the same results as from the 1st call. – πάντα ῥεῖ Jun 23 '16 at 18:10
  • awesome .. I knew some thing like that has to happen ... but u explained clearly ... thank you :) – pokche Jun 23 '16 at 18:11
3
static mySingleton *ptr;

inside the class definition is just a declaration. It is not a definition. You need to define it using:

mySingleton * mySingleton::ptr = nullptr;

outside the class definition.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • @R Sahu So I cannot define the ptr inside the class static mySingleton *ptr = nullptr; .. cause when I do that I get compilation error. – pokche Jun 23 '16 at 17:47
  • 2
    @pokche But it doesn't even have to be a static *member*. It could be a static variable inside of the `getInstance` function. – juanchopanza Jun 23 '16 at 17:52
  • 4
    @pokche And it doesn't even have to be a pointer to a dynamically allocated object either :-) – juanchopanza Jun 23 '16 at 17:54