Guest User

Untitled

a guest
Jun 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. int i = 3;
  2. int& const j = i;
  3.  
  4. void do_something_with_fatty_class(FattyClass c);
  5.  
  6. void do_something_with_fatty_class(FattyClass *c);
  7.  
  8. do_something_with_fatty_class(0);
  9.  
  10. if (!c) return; // return if function was passed null
  11.  
  12. int i = 42;
  13. int& j = i;
  14. j = 43;
  15.  
  16. int i = 42;
  17. int k = 43;
  18. int& j = i;
  19. j = k;
  20.  
  21. struct null_pointer_exception { ... };
  22.  
  23. template<typename T>
  24. struct non_null_pointer {
  25. // No default ctor as it could only sensibly produce a NULL pointer
  26. non_null_pointer(T* p) : _p(p) { die_if_null(); }
  27. non_null_pointer(non_null_pointer const& nnp) : _p(nnp._p) {}
  28. non_null_pointer& operator=(T* p) { _p = p; die_if_null(); }
  29. non_null_pointer& operator=(non_null_pointer const& nnp) { _p = nnp._p; }
  30.  
  31. T& operator*() { return *_p; }
  32. T const& operator*() const { return *_p; }
  33. T* operator->() { return _p; }
  34.  
  35. // Allow implicit conversion to T* for convenience
  36. operator T*() const { return _p; }
  37.  
  38. // You also need to implement operators for +, -, +=, -=, ++, --
  39.  
  40. private:
  41. T* _p;
  42. void die_if_null() const {
  43. if (!_p) { throw null_pointer_exception(); }
  44. }
  45. };
  46.  
  47. int &x = *((int*)0); // Illegal but some compilers accept it
  48.  
  49. char* p = s; *p = *s; *p++ = *s++; i = ++*p;
  50.  
  51. char* p;
  52.  
  53. char *p;
  54.  
  55. int theInt = 0;
  56. int& refToTheInt = theInt;
  57.  
  58. int otherInt = 42;
  59. refToTheInt = otherInt;
  60.  
  61. int firstInt = 1;
  62. int secondInt = 2;
  63. secondInt = firstInt;
  64. firstInt = 3;
  65.  
  66. assert( firstInt != secondInt );
  67.  
  68. MyClass & c = *new MyClass();
  69.  
  70. c = *new MyClass("other")
  71.  
  72. MyClass * a = new MyClass();
  73. MyClass & b = *new MyClass();
  74. a = new MyClass("other");
  75. b = *new MyClass("another");
Add Comment
Please, Sign In to add comment