Archived

This topic is now archived and is closed to further replies.

Spiral

Singleton template problem

Recommended Posts

Spiral    122
I''ve made a template class for creating Singletons, it basically works like this:
  

class MySingleton {
public:
// stuff here


  void function() {
    // stuff

  }

protected:
// stuff here

};

//////


Singleton<MySingleton> * ptr = Singleton<MySingleton>::GetInstance();

ptr->function();

  
Now, if the user uses Singleton<MySingleton> then everything works like Singleton''s should... but the user can still create multiple objects, copy and assign etc instances of MySingleton on its own, unless the user protects the constructor , destructor, copy constructor, etc by their own accord. Is there anyway to make it so Singleton<> only accepts classes that have the constructors etc protected? I can see that Java-style interfaces would be useful here... is there a way to emulate that?

Share this post


Link to post
Share on other sites
SteveC    122
I don''t think there is any way to get the templated class to have those kinds of requirements.

I''m not sure if you can use a policy technique to enforce this rule.

Basically you are looking for a compile time check to ensure that the ctor and dtor cannot be called. But such a check would likely just generate a compiler error

Share this post


Link to post
Share on other sites
null_pointer    289
Try this:

  
template <class T> class singleton
{
public:
static T& instance () { static T t; return t; }
 
private:
singleton ();
virtual ~singleton ();

singleton (const singleton&);
singleton& operator = (const singleton&);
};


Now simply derive from singleton. Calling singleton::instance is the only way to access the instance. (That function becomes part of your class's interface through public inheritance). Look:

  
class application : public singleton <application>
{
public:
void member1 () { cout << "calling member one on: " << this << "\n"; }
void member2 () { cout << "calling member two on: " << this << "\n"; }
 
// just write the rest as usual, even public constructors

};
 
int main ()
{
application a; // error, singleton::singleton () does not exist!

 
application::instance ().member1 (); // ok

application::instance ().member2 (); // ok, too

}


You really should get Modern C++ Design by Andre Alexandrescu. It discusses policy-based class design and devotes a chapter to singletons. There is a lot to learn, and besides, there is a ton of other useful information in the book!


Edited by - null_pointer on February 8, 2002 11:53:50 AM

Share this post


Link to post
Share on other sites
Void    126
quote:
Original post by Spiral
Is there anyway to make it so Singleton<> only accepts classes that have the constructors etc protected?



You wouldn''t wwant to do that. Why limit your singleton template class?

What happens if you need to make a 3rd party class into a singleton and it so happens that that class has public ctor,dtor?

Share this post


Link to post
Share on other sites