Advertisement
Miseryk

C++ Singleton Is a Lie

Jan 6th, 2021
936
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. //VS 2019 Pro / 2013 Ultimate
  2. //The cake is a lie, patterns are a lie
  3.  
  4. #include <iostream>
  5. #include <Windows.h>
  6.  
  7. int main2();
  8.  
  9. class SystemClass
  10. {
  11. private:
  12.     int var2;
  13.  
  14.     SystemClass()
  15.     {
  16.         this->var = 0;
  17.         this->var2 = 0;
  18.     }
  19.  
  20. public:
  21.     int var;
  22.  
  23.     void SetVar(int param1)
  24.     {
  25.         this->var = param1;
  26.     }
  27.  
  28.     void SetVar2(int param1)
  29.     {
  30.         this->var2 = param1;
  31.     }
  32.  
  33.     void prn()
  34.     {
  35.         std::cout << "this works! " << this->var << std::endl;
  36.     }
  37.  
  38.     static SystemClass& getInstance()
  39.     {
  40.         static SystemClass theInstance;
  41.  
  42.         return theInstance;
  43.     }
  44.  
  45.     void prn2()
  46.     {
  47.         std::cout << "this works2! " << this->var2 << std::endl;
  48.     }
  49. };
  50.  
  51. class Singleton
  52. {
  53. private:
  54.     /* Here will be the instance stored. */
  55.     static Singleton* instance;
  56.  
  57.     int var;
  58.  
  59.     /* Private constructor to prevent instancing. */
  60.     Singleton();
  61.  
  62. public:
  63.     int var2;
  64.  
  65.     void SetVar(int param1)
  66.     {
  67.         this->var = param1;
  68.     }
  69.  
  70.     void SetVar2(int param1)
  71.     {
  72.         this->var2 = param1;
  73.     }
  74.  
  75.     void prn()
  76.     {
  77.         std::cout << "this works! " << this->var << std::endl;
  78.     }
  79.  
  80.     void prn2()
  81.     {
  82.         std::cout << "this works2! " << this->var2 << std::endl;
  83.     }
  84.  
  85.     /* Static access method. */
  86.     static Singleton* getInstance();
  87. };
  88.  
  89. /* Null, because instance will be initialized on demand. */
  90. Singleton* Singleton::instance = 0;
  91.  
  92. Singleton* Singleton::getInstance()
  93. {
  94.     if (instance == 0)
  95.     {
  96.         instance = new Singleton();
  97.     }
  98.  
  99.     return instance;
  100. }
  101.  
  102. Singleton::Singleton()
  103. {
  104.     this->var = 0;
  105.     this->var2 = 0;
  106. }
  107.  
  108. int main()
  109. {
  110.     /*__asm
  111.     {
  112.         int 3
  113.     }*/
  114.  
  115.     std::cout << "########### SINGLETON BY REFERENCE ###########" << std::endl;
  116.  
  117.     SystemClass& sys = SystemClass::getInstance();
  118.  
  119.     //SystemClass sys = SystemClass::getInstance();
  120.     sys.SetVar(333);
  121.     sys.SetVar2(666);
  122.     sys.prn();
  123.     sys.prn2();
  124.  
  125.     //Por referencia
  126.     __asm
  127.     {
  128.         ; int 3
  129.         mov eax, sys
  130.  
  131.         ; VISUAL STUDIO 2019
  132.         and dword ptr[eax - 0x04], 0x0
  133.  
  134.         ; VISUAL STUDIO 2013
  135.         ; and dword ptr[eax + 0x08], 0x0
  136.         ; int 3
  137.     }
  138.  
  139.     /*__asm
  140.     {
  141.         int 3
  142.         mov eax, SystemClass::getInstance
  143.         int 3
  144.     }*/
  145.  
  146.     //std::cout << sizeof(SystemClass) << std::endl;
  147.  
  148.     //ZeroMemory(SystemClass, 4);
  149.  
  150.     sys = SystemClass::getInstance();
  151.     sys.prn();
  152.     sys.prn2();
  153.  
  154.     main2();
  155.  
  156.     std::cin.get();
  157.  
  158.     return 0;
  159. }
  160.  
  161. int main2()
  162. {
  163.     std::cout << std::endl;
  164.     std::cout << "########### SINGLETON BY POINTER ###########" << std::endl;
  165.  
  166.     Singleton* s = Singleton::getInstance(); // Ok
  167.  
  168.     std::cout << s << std::endl;
  169.  
  170.     s->SetVar(333);
  171.     s->SetVar2(666);
  172.     s->prn();
  173.     s->prn2();
  174.  
  175.     __asm
  176.     {
  177.         lea eax, [Singleton::instance]
  178.         and dword ptr[eax], 0x0
  179.     }
  180.  
  181.     Singleton* r = Singleton::getInstance();
  182.  
  183.     std::cout << r << std::endl;
  184.  
  185.     r->prn();
  186.     r->prn2();
  187.  
  188.     return 0;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement