Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int i = 3;
- int& const j = i;
- void do_something_with_fatty_class(FattyClass c);
- void do_something_with_fatty_class(FattyClass *c);
- do_something_with_fatty_class(0);
- if (!c) return; // return if function was passed null
- int i = 42;
- int& j = i;
- j = 43;
- int i = 42;
- int k = 43;
- int& j = i;
- j = k;
- struct null_pointer_exception { ... };
- template<typename T>
- struct non_null_pointer {
- // No default ctor as it could only sensibly produce a NULL pointer
- non_null_pointer(T* p) : _p(p) { die_if_null(); }
- non_null_pointer(non_null_pointer const& nnp) : _p(nnp._p) {}
- non_null_pointer& operator=(T* p) { _p = p; die_if_null(); }
- non_null_pointer& operator=(non_null_pointer const& nnp) { _p = nnp._p; }
- T& operator*() { return *_p; }
- T const& operator*() const { return *_p; }
- T* operator->() { return _p; }
- // Allow implicit conversion to T* for convenience
- operator T*() const { return _p; }
- // You also need to implement operators for +, -, +=, -=, ++, --
- private:
- T* _p;
- void die_if_null() const {
- if (!_p) { throw null_pointer_exception(); }
- }
- };
- int &x = *((int*)0); // Illegal but some compilers accept it
- char* p = s; *p = *s; *p++ = *s++; i = ++*p;
- char* p;
- char *p;
- int theInt = 0;
- int& refToTheInt = theInt;
- int otherInt = 42;
- refToTheInt = otherInt;
- int firstInt = 1;
- int secondInt = 2;
- secondInt = firstInt;
- firstInt = 3;
- assert( firstInt != secondInt );
- MyClass & c = *new MyClass();
- c = *new MyClass("other")
- MyClass * a = new MyClass();
- MyClass & b = *new MyClass();
- a = new MyClass("other");
- b = *new MyClass("another");
Add Comment
Please, Sign In to add comment