Reputation:
I'm implementing Singleton the way described in the first answer here. The getInstance() method returns a reference but what I'm not sure about is how I should create my new instance and check if it already exists before creating it and returning it.
class Song {
private:
Song song;
Song();
Song(Song const&); // don't implement
void operator = (Song const&); //don't implement
public:
static Song &getInstance();
}
So what should my getInstance();
implementation look like? I want to return the song member object if it already exists and otherwise create it and than return it. I know there's a n implementation in the link I added but I'm not sure it does what I want and I don't understand it very well.
Also, could someone explain what these two lines are for and why the =
operator is being overwritten?
Song(Song const&); // don't implement
void operator = (Song const&); //don't implement
Upvotes: 0
Views: 3259
Reputation: 227518
The implementation is in the answer you quote:
static S& getInstance()
{
static S instance; // Guaranteed to be destroyed.
// Instantiated on first use.
return instance;
}
The instance is static, so it gets instantiated at the first call of the getInstance()
function.
See this related SO question for more information.
A note on the second question. The first is the copy constructor:
Song(Song const&);
It is needed if you want to initialize an object to take the value of another one:
Song a;
Song b = a; // calls copy constructor
The second is an assignment operator:
void operator=(const Song&);
needed for assigning a value to an already existing instance:
Song a;
Song b;
b = a; // calls assignment operator.
Note that the standard return value for an assignment operator is a reference:
Song& operator=(const Song&);
Upvotes: 1
Reputation: 254691
So what should my getInstance(); implementation look like? I want to return the song member object if it already exists and otherwise create it and than return it.
That's exactly what the code you link do does. To quote that answer:
static S& getInstance()
{
static S instance; // Guaranteed to be destroyed.
// Instantiated on first use.
return instance;
}
As the comment says, it is instantiated on first use; that's how static variables at function scope work.
The deathtrap associated with this particular attempt to implement the Singleton anti-pattern is that it's possible to access it after it's destroyed, for example from the destructor of another static object.
Also, could someone explain what these two lines are for and why the = operator is being overwritten?
These are to prevent copying the object. Copying can be done with either a copy constructor (creating a new object) or a copy-assignment operator (overwriting an existing object); if these are declared private and not implemented, then the instance can't be copied. In C++11, this can be made more explicit by deleting them:
Song(Song const&) = delete;
void operator = (Song const&) = delete;
Upvotes: 1
Reputation: 8815
To answer the second question, those two lines declare the copy constructor and assignment operator as private, so that they can't be called. Without those two lines, the compiler would generate default implementations of these, and clients could create copies, defeating the purpose of the Singleton
pattern.
Upvotes: 0