I have written following Singleton class code,
class Singleton
{
private:
Singleton(){}
public:
static Singleton& getInstance()
{
static Singleton instance; //made it static so only initialized once
return instance;
}
};
Now I tried to use it,
int main(int argc, char* argv[])
{
Singleton s = Singleton::getInstance();
Singleton s1 = Singleton::getInstance();
cout<<&s << " " << &s1<<endl; //why getting different address here?
return 0;
}
Both s,s1 gives different addresses.
Sample Output:
001AFECB 001AFEBF
I am using Visual Studio 2012. I made instance object static, so it should be initialized only once, and it should not die.