Jump to content

Recommended Posts

Hi

I am trying to implement a pattern known as the singleton which only allows 1 version of a class to be initailised here is the code (Note this is a Visual C++ implementation that I want to convert to BoostC++):

 

//Singelton .h

class device{

public:

device(void){deviceIndex=0; cout<< "creating Device "<<endl;};

~device(void){cout<<"device object is being destroyed: "<<endl;};

};

 

class Timer: public device

{

public:

Timer(void){cout<< "creating timer "<<endl;}

~Timer(void){cout<<"Timer object is being destroyed: "<<endl;}

};

 

class Timer3: public Timer

{

static Timer3 *pT3Instance; //SHOULD THIS BE static class Timer3* pT3Instance ?

Timer3(void){cout<< "creating timer3 "<<endl; }

public:

 

static Timer3* createTimer3(void);

 

~Timer3(void){}//{delete T3Overflow; cout<<"Timer3 object is being destroyed: "<<endl;}

 

};

 

 

//Singleton.cpp file

#include "singleton.h"

 

Timer3* Timer3::pT3Instance;

 

Timer3* Timer3::createTimer3(void)

{

if(pT3Instance==0)

{

pT3Instance= new Timer3();

cout<<"creating timer3 "<<pT3Instance<<endl;

}else{ cout<<"Timer3 already allocated exiting "<<pT3Instance<<endl;}

return pT3Instance;

}

 

//Test.cpp

#include "singleton.h"

 

Timer3* T3;

Timer3* T4;

int main(int argc, char* argv[])

{

T3=Timer3::createTimer3(); //THIS IS ONE AREA THAT I AM UNSURE HOW TO CODE WITH BoostC++

 

cout<< " Device Index= "<<T3->getDeviceIndex()<<endl;

T4=Timer3::createTimer3();

 

return 0;

}

 

Sorry about the cout statements this is a Visual C++ version that I am trying to convert to BoostC++ which has been unhappy with the C++ syntax shown here. Does anyone know how to convert this to BoostC++ so it compiles.

Many thanks

Dave

Link to post
Share on other sites
  • 1 month later...

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...