
Envoyé par
Mat007
Faut pas se voiler la face, comme il a été dit, un singleton c'est une variable globale...
MAT.
Oui, alors pour pas passer trop de temps, j'ai gardé cette implémentation :
CApp.h :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class CApp
{
private:
static CApp* m_pApp;
CApp( unsigned short port, const std::string& add, const std::string& logDir) : ... {}
public:
~CApp() {}
static CApp* CreateInstance( unsigned short port, const std::string& add, const std::string& logDir) {
return ( m_pApp = new CApp(port, add, logDir) );
}
static CApp* GetInstance() {
return m_pApp;
}
void DeleteInstance() {
delete m_pApp;
}
}; |
CApp.cpp:
CApp* CApp::m_pApp = NULL;
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| int main()
{
//Get the general parameters stored in our database
unsigned short port = 2001;
std::string add= "192.168.192.250";
std::string logDir = ".";
{
CDatabase db("MSA");
if( db.Open() == 0 )
db.GetParams( port, add, logDir );
}
//Create our unique global object
CApp* pApp = CApp::CreateInstance( port, add, logDir );
//Init our unique object instance
if( pApp && pApp->Init() == 0 )
{
//Wait for the end ...
pApp->WaitForExit();
}
//Delete the unique object
pApp->DeleteInstance();
return 0;
} |
Partager