I have a My_Singleton
global object singleton_obj
that I want to ensure that it is properly initialized before it is used and there is only a single instance of it. I have learned about Nifty counter here, and I found some other answers about including std::ios_base::Init
object in the class can achieve the same effect.
/*My_Singleton.h */
#include <iostream>
class My_Singleton
{
My_Singleton(My_Singleton&&) =delete;
My_Singleton& operator=(My_Singleton&&) =delete;
My_Singleton(My_Singleton const&) =delete;
My_Singleton& operator=(My_Singleton const&) =delete;
std::ios_base::Init initalizer;
public:
My_Singleton() =default;
~My_Singleton() =default;
};
extern My_Singleton singleton_obj;
/*My_Singleton.cpp*/
#include "My_Singleton.h"
My_Singleton singleton_obj;
My question are:
1. Is this the correct way of doing it? If not, please provide a fix
2. How does std::ios_base::Init
implemented?