For a class that has an explicit constructor with 2 parameters, I'm thinking to rewrite my Singleton. This idea is because the "getInstance()" method of Singleton. If the implementation of this method is like this:
Every time I call getInstance, I need to pass parameters. For the first time it's ok, but for another calls it's not fine to do this.Code:static Singleton<T>::_instance = NULL; template <typename T> template <typename U, typename X > T* Singleton<T>::getInstance( U, X ) { if( _instance == NULL ) _instance = new T( U, X ); return _instance; }
But if I have a Singleton that receives the parameters and verifies inside their constructor, I think it works.Code:class MyClass : public Singleton<MyClass> { public: explicit MyClass( int, char ); ... }
Ok, the problem for me is, is a good way to solve the explicit parameters in constructor with this solution? Is there a better way to do?Code:template< typename T > class Singleton { public: template <typename X, typename U> Singleton() { if( _instance == NULL ) { _instance = new T( X, U ); } T* getInstance() { return _instance; } } private: static T* _instance; };
Any help is very appreciated.
Thanks




Reply With Quote