Advertisement
fcamuso

Design Patterns lezione 02

Jun 26th, 2022
956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. //Credits: Scott Meyers
  6. class Singleton {
  7.     public:
  8.  
  9.       static Singleton& getIstanza() {
  10.  
  11.         static Singleton istanza_;
  12.  
  13.         return istanza_;
  14.       }
  15.  
  16.  
  17. //TEST COPY CONSTRUCTOR
  18. //      void f1()
  19. //      {
  20. //        f2(getIstanza());
  21. //      }
  22. //
  23. //      void f2(Singleton copia_del_singleton)
  24. //      {
  25. //        //in questo momento esisterebbero DUE istanze di un singleton
  26. //      }
  27. //      void f(Singleton copia_del_singleton)
  28. //      {
  29. //        //in questo momento esisterebbero DUE istanze di un singleton
  30. //      }
  31.  
  32.  
  33. //TEST COPY ASSIGNMENT
  34. //     void f()
  35. //     {
  36. //        Singleton copia = getIstanza();
  37. //     }
  38.  
  39.      private:
  40.       Singleton() { cout << "Invocato costruttore" << endl; }
  41.  
  42.       //Singleton(const Singleton&) = delete;
  43.  
  44.       Singleton& operator=(const Singleton&) = delete;
  45.       ~Singleton() { cout << "Invocato distruttore" << endl; }
  46.  
  47. };
  48.  
  49.  
  50. int main()
  51. {
  52.   Singleton& il_singleton = Singleton::getIstanza();
  53.   Singleton& un_altro_singleton = Singleton::getIstanza();
  54.  
  55.   //il_singleton.f1();
  56.   //f(il_singleton); //no distruttore privato!
  57.   //il_singleton.f(il_singleton);
  58.  
  59.  
  60.   //VARIANTE PER MASSIME PERFORMANCE
  61.   //cercare: c++ singleton pimpl implementation
  62.  
  63.     return 0;
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement