How to work with static references in C++?

A static class is a DB there is a link that, in theory, need to not constantly open-close database connection. (one open database for the entire program)
class DB{
 static bool createConnection();
 static QSqlDatabase *pdb;

The compiler requires its description.
Where and how can I describe the program?

QSqlDatabase DB::pdb = NULL;
int main(int argc, char *argv[])

not working
September 19th 19 at 12:27
2 answers
September 19th 19 at 12:29
QSqlDatabase * DB::pdb = NULL;
September 19th 19 at 12:31
Better redo on this option:
static QSqlDatabase* getPDB()
 static QSqlDatabase *pdb = NULL;
 if(pdb == NULL) {
 pdb = connect();
 return pdb;

Well, respectively, connect should create that same connection and return the same pointer.
And no global class members...
Thank you. Take note! - jamil_Ter commented on September 19th 19 at 12:34
just a little change and the result will be singleton Myers - nikki_Bashirian commented on September 19th 19 at 12:37
Meyers suggests a singleton, initialized variable, then the possible NULL until the connection is established. - lesly.Daugherty commented on September 19th 19 at 12:40

Find more questions by tags C++