Singleton pattern confusion in C++


I have been trying to implement a singleton pattern example but it’s causing a lot of difficulties, below is my example:

// Singleton.h
class  Singleton {
    Singleton() {}
    int a, b;
    static Singleton *obj;
    Singleton(const Singleton &);
    static Singleton &getObject();

// Singleton.cpp
Singleton *Singleton::obj = 0;

// Need to keep copy constructor as it is.
Singleton::Singleton(const Singleton &other) {
    // to copy the a and b values
Singleton &Singleton::getObject() {
    return obj;

int main() {
    Singleton obj = Singleton::getObject();
    court << &obj << endl;
    court << &Singleton::getObject(); << endl;
    return 0;

In the above example, the address of both objects is different in the output, why? How can I solve this?

Thanks in advance and looking forward.

Merry Christmas everyone.

Greetings 2k10cs86!

We see that this is your first time posting in our forums! :medal_sports: We welcome you to the Unreal Engine Community! I see that you are having issues with implementing a singleton pattern. May I ask, have you coded in Unity prior to Unreal Engine? Which version of UE are you currently using? I have looked around (simple Google search), and it seems that other game devs have chosen a different route instead of traditional singleton patterns in UE. Here is a link to a discussion if you would like to see how others have approached issues with the singleton pattern: Singleton pattern in c++ - UE4 AnswerHub I hope this helps or points you in the right direction! Happy Creating and Merry Christmas! :christmas_tree:

Hi @2k10cs86 , could you share me what’s the unreal engine use case here ? This problem can be posted in Stackoverflow too. However if you want to see a singleton example in the engine,

Download and compile the source code and see GameInstance class , debug it, you can find singleton examples from there.

Ohh sorry, I really don’t know about unreal engine, this is just a simple c++ program, and not related to any kind of engine.

Well, I think the value of “obj” is undetermined as you are not initializing it anywhere.
It’s a pointer…

Explain C++ Singleton design pattern. (

Note the “new” inside “getInstance”.

Sorry, it was typo, I have corrected the real post.