fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. static void print_addr(const char *type, const void *p){
  6. cout << right << setfill(' ');
  7. cout << setw(32) << type << ": ";
  8. cout << left << "I'm " << p << endl;
  9. }
  10.  
  11. class with_ref{
  12. public:
  13. void whoami(void) const{
  14. print_addr(__PRETTY_FUNCTION__, this);
  15. }
  16. void whoami(void){
  17. print_addr(__PRETTY_FUNCTION__, this);
  18. static_cast<const with_ref &>(*this).whoami();
  19. }
  20. };
  21.  
  22. class without_ref{
  23. public:
  24. void whoami(void) const{
  25. print_addr(__PRETTY_FUNCTION__, this);
  26. }
  27. void whoami(void){
  28. print_addr(__PRETTY_FUNCTION__, this);
  29. static_cast<const without_ref>(*this).whoami();
  30. }
  31. };
  32.  
  33. int main() {
  34. with_ref with_ref_inst;
  35. with_ref_inst.whoami();
  36.  
  37. without_ref without_ref_inst;
  38. without_ref_inst.whoami();
  39. return 0;
  40. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
         void with_ref::whoami(): I'm 0x7ffdcfb6d0cd
   void with_ref::whoami() const: I'm 0x7ffdcfb6d0cd
      void without_ref::whoami(): I'm 0x7ffdcfb6d0ce
void without_ref::whoami() const: I'm 0x7ffdcfb6d0cf