I have used singleton calss following the example:
But i get the error as “Unresolved external symbols”
this is the code i tried out:
#include<iostream>
using namespace std;
class singleton
{
int value;
static singleton *instance;
protected:
singleton()
{
value=0;
}
public:
static void initialize()
{
if(instance==NULL)
singleton();
else
cout<<"An instance of singleton already exist...";
}
static singleton& getInstance()
{
return *instance;
}
int getValue()
{
return value;
}
};
void main()
{
singleton::initialize();
}
A little bit explanation on Singleton classes would be really great. The scenario its used. advantages and drawbacks. Alternatives to Singleton. etc
For a start, I think:
should be:
The way you have it, you’re not actually storing the newly instantiated object so
instancewill always be null.It’s also good form to explicitly set statics with:
(outside the class definition).
In fact, it’s possibly better to start with the baseline singleton code and work your way up from there. This is a standard-form pointer version:
You can see that both pointers are the same from the output:
Or the reference version, since that seems to be what you’re aiming for: