Advertisement
Guest User

Untitled

a guest
Dec 9th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. class Service {
  2. public:
  3.     Service() {
  4.         if(s_count++ == 0) {
  5.             s_initialize();
  6.         }
  7.     }
  8.    
  9.     ~Service() {
  10.         if(--s_count == 0) {
  11.             s_terminate();
  12.         }
  13.     }
  14.    
  15.     void foo() {
  16.         // do something that requires the resource/system here
  17.     }
  18.    
  19. private:
  20.     static int s_count;
  21.    
  22.     static void s_initialize() {
  23.         // initialize some resource/system here
  24.     }
  25.    
  26.     static void s_terminate() {
  27.         // terminate some resource/system here
  28.     }
  29. };
  30.  
  31. int Service::s_count = 0; // this is BS by the way C++
  32.  
  33.  
  34.  
  35. int main() {
  36.     // initializes
  37.     Service service;
  38.    
  39.     // just reference counts
  40.     Service redundant;
  41.    
  42.     // operates on the service
  43.     service.foo();
  44.    
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement