Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. struct Osoba
  6. {
  7. string imie, nazwisko;
  8. int wiek;
  9. };
  10.  
  11. template <typename T>
  12. class Box
  13. {
  14. private:
  15. T*wsk;
  16.  
  17. public:
  18. Box():wsk(new T){}
  19. Box(T *a):wsk(new T(*a)){}
  20. Box(const Box&a):wsk(new T(*a.wsk)){}
  21.  
  22. Box&operator=(const Box&a)
  23. {
  24. if(this!=&a)
  25. {
  26. delete wsk;
  27. wsk=new T(*a.wsk);
  28. }
  29. return *this;
  30. }
  31.  
  32. T*operator->()
  33. {
  34. return wsk;
  35. }
  36.  
  37. T&operator*()
  38. {
  39. return *wsk;
  40. }
  41.  
  42.  
  43. ~Box()
  44. {
  45. delete wsk;
  46. wsk=0;
  47. }
  48. };
  49.  
  50. int main()
  51. {
  52. Box<int>w;
  53. Box<double>w2;
  54. Box<Osoba>w3;
  55. cout<<*w<<" "<<w3->wiek;
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement