I have taken over a piece of code developed by a contractor. This code appears to run fine on him machine from Visual Studio, however when I try the same code, from the same version of VS, on my machine I get an error when it makes a call to Activator::CreateInstance. Code below. Does anyone know why this might be happening? Is he using the function correctly? Is there not a standard singleton class? surprised he has created his own!
generic< class T > public ref class Singleton abstract
{
public:
/*
* Create an instance of the singleton class
* @return A handle to the singleton class
*/
static T Instance()
{
// Check if the object has already been created
if( m_Instance == nullptr )
{
// ####### Runtime error here #######
m_Instance = safe_cast< T >( System::Activator::CreateInstance( T::typeid ) );
}
return( m_Instance );
}
protected:
/* Default Constructor */
Singleton< T >()
{
}
/* Copy Constructor */
Singleton< T >( const T )
{
}
/* Assignment Operator */
const T operator = ( const T )
{
return( safe_cast< const T >( this ) );
}
private:
// Handle to the single instance of the class created in the system
static T m_Instance;
};