ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to create a singleton class. I think I have been able to code it correctly after spending sometime learning a thing or two I don't know very well about C++.
Anyway.... I have put this class in a so. But when I start the application that uses this library, it complais because it can't find one symbol (one the class' static members).
although i much prefer returning a refernce instead of a pointer, but this depends of the lifetime of the singleton. I use a template class for singletons which have the life time of the app and register an atexit func to delete the instance; where getinstance returns a ref.
also you may want to include a few funcs in private so that they are not auto created.
I get exactly the same error... only that in compiletime, instead of runtime.
Code:
smiledb.o: In function `SmileDB::getInstance()':
smiledb.cpp:(.text+0x29): undefined reference to `SmileDB::instance'
smiledb.cpp:(.text+0x5a): undefined reference to `SmileDB::instance'
I think it's a problem in my code. Can you show me one very simple example of a singleton (c++) so that I can extend from it? both the .h and the .cpp
With just a getInstance() will be enough.. and thanks for your help so far.
Now... about the dmail's way... I read somewhere that this way is better, cause it avoids memory leaks.. could you elaborate on this, please? (I'm going to give it a try right away.... the marvels of using a CVS )
Now I have a little trickier problem. How can you make a singleton that can be extended? Say, you have a number of classes that you want to make singletons, so you have a single instance of each one of them. I don't want to have to write the same code over and over again in each one. Is there an easy way to accomplish it?
#ifndef _SINGLETON_H_
# define _SINGLETON_H_
namespace pattern
{
template< class T >
class Singleton
{
public:
inline static T& manager(void)//getter for the singleton
{
if( ! m_instance )//check
{ //create the instance of the derived class
m_instance = new T();
//register the function to be called at exit
atexit( Pattern::Singleton<T>::destroy );
}
return (*m_instance);//return the reference
}
protected:
//called when the app exits
static void destroy(void)
{
if(m_instance){ delete m_instance; m_instance = 0; }
}
Singleton(void){;}//constructor
~Singleton(void){;}//destructor
private:
static T* volatile m_instance;//the instance of the
//declare two other functions so the compiler doesn't auto create
//them, these are in private so that there is no way of calling them
Singleton(const Singleton&){;}
Singleton& operator=(const Singleton&){;}
};
};//namespace pattern
//intailise the static
template<class T>T* volatile Pattern::Singleton<T>::m_instance = 0;
#endif //_SINGLETON_H_
and to inherit from this use something like
Code:
class Singleton_timer : public Pattern::Singleton<Singleton_timer>
{
public:
//public func here
private:
friend class Pattern::Singleton<Singleton_timer>;
Singleton_timer( void ){;}
~Singleton_timer( void ){;}
};
//typedef a smaller name for this class
typedef Pattern::Singleton<Singleton_timer> Timer;
Therefore a singleton timer would be called like so:
It works... but I have a problem. I have to set the contructor of the extending class as public, because if I set it to private, it wont compile:
Code:
../comun/formas/login/../../smiledb/../singleton.h: In static member function 'static T& Smile::Singleton<T>::getInstance() [with T = Smile::SmileDB]':
../comun/formas/login/login.cpp:27: instantiated from here
../comun/formas/login/../../smiledb/smiledb.h:16: error: 'Smile::SmileDB::SmileDB()' is private
../comun/formas/login/../../smiledb/../singleton.h:40: error: within this context
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.