fork(2) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct MyClass {
  5. int n;
  6. int bar() const { return n; }
  7. };
  8.  
  9. int foo(MyClass const* aPtr = 0) {
  10. MyClass const& a = aPtr ? *aPtr : MyClass(); // Either bind to *aPtr, or to a default-constructed MyClass
  11. cout << "a refers to "<< (void*)&a<<" ";
  12. return a.bar();
  13. }
  14.  
  15. int main() {
  16. MyClass mya;
  17. cout << "With nullptr: ";
  18. foo();
  19. cout <<endl<<"With object " << (void*)&mya<<": ";
  20. foo(&mya);
  21. cout << "\nIf the object pointer and the referred pointer are different, a is not bount to the original object\n";
  22. }
  23.  
Success #stdin #stdout 0s 3096KB
stdin
Standard input is empty
stdout
With nullptr: a refers to 0xbfdbbf0c 
With object 0xbfdbbf3c: a refers to 0xbfdbbf0c 
If the object pointer and the referred pointer are different, a is not bount to the original object