Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. #ifndef SMARTPOINTER_HPP__
  2. #define SMARTPOINTER_HPP__
  3.  
  4. #include <iostream>
  5.  
  6. template<typename T>
  7. class sp {
  8. public:
  9. sp(T* ptr) : ptr_(ptr), ref_cnt_(1) {
  10. std::cout << "sp ctor: " << ptr_ << ", ref_cnt_: " << ref_cnt_ << std::endl;
  11. }
  12.  
  13. T& operator*() { return *ptr_; }
  14.  
  15. T* operator->() { return ptr_; }
  16.  
  17. sp(const sp<T>& rhs) : ptr_(rhs.ptr_), ref_cnt_(rhs.ref_cnt_) {
  18. ++ref_cnt_;
  19. std::cout << "sp copy ctor: " << ptr_ << ", ref_cnt_: " << ref_cnt_ << std::endl;
  20. }
  21.  
  22. sp& operator=(const sp& rhs) {
  23. if(this != &rhs) {
  24. *this = rhs;
  25. ++ref_cnt_;
  26. std::cout << "sp assignment: " << ptr_ << ", ref_cnt_: " << ref_cnt_ << std::endl;
  27. }
  28. return *this;
  29. }
  30.  
  31. ~sp() {
  32. if(ptr_)
  33. std::cout << "~sp: " << ptr_ << ", ref count: " << ref_cnt_ << std::endl;
  34. --ref_cnt_;
  35. if(ref_cnt_ == 0)
  36. delete ptr_;
  37. }
  38.  
  39. bool operator==(const T& that) const { return ptr_ == that.ptr_; }
  40. bool operator==(T* that) const { return ptr_ == that; }
  41.  
  42. bool operator!=(const sp& that) const { return ptr_ != that.ptr_; }
  43. bool operator!=(sp* that) const { return ptr_ != that; }
  44.  
  45.  
  46. private:
  47. T* ptr_;
  48. unsigned ref_cnt_;
  49. };
  50.  
  51.  
  52. #endif //SMARTPOINTER_HPP__
  53.  
  54. #include <iostream>
  55. #include <string.h>
  56.  
  57. #include "smart_pointer.hpp"
  58.  
  59. class name {
  60. public:
  61. name(const char* label) : label_(label) {}
  62.  
  63. const char* get_name() const { return label_; }
  64. unsigned length() const { return strlen(label_); }
  65.  
  66. private:
  67. const char* label_;
  68. };
  69.  
  70. int main() {
  71.  
  72. {
  73. std::cout << "Test with intsn";
  74. sp<int> myptr1 = new int(3);
  75. *myptr1 = 4;
  76. std::cout << "number: " << *myptr1 << std::endl;
  77.  
  78. sp<int> myp2(myptr1);
  79.  
  80. sp<int> myp3(myptr1);
  81.  
  82. sp<int> myp4 = myptr1;
  83. }
  84.  
  85.  
  86. {
  87. std::cout << "Test with name objectn";
  88. sp<name> myname = new name("Andrew");
  89. std::cout << myname->get_name() << " has length: " << myname->length() << std::endl;
  90. sp<name> myname2(myname);
  91. }
  92.  
  93. return 0;
  94. }
  95.  
  96. Test with ints
  97. sp ctor: 004C7A80, ref_cnt_: 1
  98. number: 4
  99. sp copy ctor: 004C7A80, ref_cnt_: 2
  100. sp copy ctor: 004C7A80, ref_cnt_: 2
  101. sp copy ctor: 004C7A80, ref_cnt_: 2
  102. ~sp: 004C7A80, ref count: 2
  103. ~sp: 004C7A80, ref count: 2
  104. ~sp: 004C7A80, ref count: 2
  105. ~sp: 004C7A80, ref count: 1
  106. Test with name object
  107. sp ctor: 004C7A80, ref_cnt_: 1
  108. Andrew has length: 6
  109. sp copy ctor: 004C7A80, ref_cnt_: 2
  110. ~sp: 004C7A80, ref count: 2
  111. ~sp: 004C7A80, ref count: 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement