Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a general use class from all over project

Tags:

c++

In our application there is general used classes( e.g a class which holds project configuration parameters ) like below .

class DB{   
public:
int projectVersion
int somethingSoMuchImportantAllOverTheProject
};
extern DB* pDB;

Because all other classes will use this class, all of them needs to access it somehow . Our solution is putting "extern DB* pDB;" under its header file so whoever needs it, will be able to access it after including the header . I am thinking maybe there is a better solution and want to get your ideas for this general case.

like image 495
Kadir Erdem Demir Avatar asked Nov 13 '22 11:11

Kadir Erdem Demir


1 Answers

It's a singleton. You can find lots of discussions on it, but one important thing is, hide the constructor of the class so users of the class are forced to use the one object.

like image 128
mark Avatar answered Nov 15 '22 04:11

mark