Advertisement
Aslai

Memory Counter

Feb 10th, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. template<class type>
  2.         class counter{
  3.             type** myptr;
  4.             unsigned int *count;
  5.  
  6.             void countpp(){
  7.                 (*count) ++;
  8.             }
  9.             void countss(){
  10.                 (*count)--;
  11.                 if( (*count) == 0 ){
  12.                     delete count;
  13.                     delete *myptr;
  14.                     delete myptr;
  15.                 }
  16.             }
  17.             public:
  18.             counter(type *t){
  19.                 myptr = new type*;
  20.                 count = new unsigned int;
  21.                 *myptr = t;
  22.                 (*count) = 1;
  23.             }
  24.             counter( const counter<type> &t ){
  25.                 myptr = t.myptr;
  26.                 count = t.count;
  27.                 countpp();
  28.             }
  29.             ~counter(){
  30.                 countss();
  31.             }
  32.             type*& get(){
  33.                 return *myptr;
  34.             }
  35.             type*& operator->(){
  36.                 return *myptr;
  37.             }
  38.             type& operator*(){
  39.                 return **myptr;
  40.             }
  41.             type& operator[](int idx){
  42.                 return (*myptr)[idx];
  43.             }
  44.             counter<type>& operator=(counter<type> & t){
  45.                 countss();
  46.                 myptr = t.myptr;
  47.                 count = t.count;
  48.                 countpp();
  49.                 return *this;
  50.             }
  51.  
  52.         };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement