Your getInstance() static member is not the best for making a singleton class instance. It just returns a pointer. This is bad, because there is nothing stopping the user from calling getInstance multiple times and receiving multiple class instance pointers.
Another solution could be like so:
1 2 3 4 5 6 7 8 9
class test
{
public:
test* get_singleton() const {if(!Singleton) Singleton = new test; return Singleton;}
private:
test() = default; //Private default ctor
staticinline test* Singleton = nullptr; //Single static member
};
Now whenever you need access to the singleton instance, you can use the get_singleton() method to access it. Also, you should try to avoid operator new in modern C++ especially with the advent of alternatives like smart pointers. I think you could also do this:
1 2 3 4 5 6 7 8 9
class test
{
public:
test get_singleton() const {return Singleton;}
private:
test() = default; //Private default ctor
static test Singleton;; //Single static member
};
test test::Singleton{};