djwilliams 0 Posted July 1, 2008 Report Share Posted July 1, 2008 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 Quote Link to post Share on other sites
Pavel 0 Posted July 2, 2008 Report Share Posted July 2, 2008 This code won't work. BoostC++ does not support static members yet (It's on our work list). Regards, Pavel Quote Link to post Share on other sites
Pavel 0 Posted September 2, 2008 Report Share Posted September 2, 2008 Both static variable members and static function members are now implemented in BoostC++. This feature will be available in the next release. Regards, Pavel Quote Link to post Share on other sites
Recommended Posts
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.