#include <iostream>
class Singleton {
private:
static Singleton* s_instance;
public:
static Singleton& Get() {
return *s_instance;
}
void Hello() {
std::cout << "Hey Bro" << std::endl;
}
};
Singleton* Singleton::s_instance = nullptr;
int main() {
Singleton::Get().Hello();
return 0;
}
And it prints the output from the member function Hello(). How can a nullptr be dereferenced in the static member function Get()
P.S: This code snippet was taken from the Cherno C++ series on YouTube.
nullptrcauses undefined behaviour. The meaning of "undefined" in C++ is essentially "the C++ standard does not place any constraints on what happens". Practically, that doesn't require a runtime error, or anything else - it is within the bounds of undefined behaviour that it can seem to "work" - for any definition a programmer may place on "work".