Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello All,

Please let me know what is speciality about generic singleton ?

Is it a Singleton template class ? if yes then what is the difficulty in creating an instance of some class, e.g., class A{}; using that generic singleton template ?

Regards

What I have tried:

i created a generic singleton template class but facing difficulty in creating an instance of some class. e.g., class A{}; using that template.
Posted
Updated 17-Aug-16 1:30am
Comments
Richard MacCutchan 16-Aug-16 6:03am    
What template are you talking about? Please add some proper detail to your question.
iacharyya 18-Aug-16 4:56am    
Well Richard, what I mean is given below :

Well what I did is below :

template <class T>
class Singleton{
private :
static T *instance;
Singleton(){}
Singleton(const Singleton&);
public:
static T* getInstance(); // create new T instance here

This is my template for generic Singleton class.

Now for example for class A, like :
class A{
};

I am going to create an instance of class A which has to be a singleton and I have to use this Singleton template, like,

int main()
{
Singleton<A>::getInstance(); // should create a singleton instance of class //A using generic Singleton template that is //mentioned above. I need help here, I know Singleton and all the stuff related //that, my question is generic singleton pattern, hope what i tried to explain //gives you clear picture

}
Richard MacCutchan 18-Aug-16 6:28am    
I do not think you can do it that way. The template (and thus the class) is named Singleton, so that is what you must declare in your code. But the type T has no meaning. Why not just code it as normal, since the template does not really offer anything useful.
Philippe Mori 17-Aug-16 11:37am    
You can easily find code for a singleton using your preferred search engine...

By the way, singleton is now essentially considered an anti-pattern (that is, something that should usually be avoided). Again, you can easily find information on the web.

1 solution

ok singleton class is class that you can instantiated only one object overall application or service and has private contractor
template like
C++
class Singleton
{
public:
static Singleton * getInstance()
{
  if(single==NULL)
{
  single = new Singleton();
}
return single;
}
private:
Singleton();
// some variable 
static Singleton * single;
} ;
 
Share this answer
 
v3
Comments
Philippe Mori 17-Aug-16 11:43am    
Improper indentation and some errors in code like the undefined constructor.

And the implementation here is very basic (not multiple thread safe, memory leak, no template, old style NULL...)
Mahmoud_Gamal 18-Aug-16 3:56am    
thanks i correct the error
singleton has private constructor and at least one static function and i just write
template

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900