I'm confused about the singleton pattern. To my understanding,
- It is a class that allows only one object to be created from it.
- To do this, the constructor, copy constructor, and assignment operator overloading function are made private to prevent any other objects from being created.
- The one and only instance is made public in the class and is static. Its default value is NULL.
- A function, usually called getInstance() or something similar, is used to allocate memory for the static object if it hasn't been done already
So my question is what do the constructors and copy constructor and &operator=() functions look like? Also will the destructor be public or private?
class Database {
private:
Database() {
//???
}
Database(const Database &db) {
//???
}
Database &operator=(const Database &db) {
//???
}
//~Database() {...}
public:
static Database database = NULL;
Database &getDB() {
if (database == NULL) {
database = new Database();
return database;
}
return database;
}
~Database() {...}
}