fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct a {
  5. a(int i = 0) : i(i) {}
  6. int i;
  7. friend ostream& operator <<(ostream& packet, const a *t);
  8. friend ostream& operator <<(ostream& packet, const a &t);
  9. friend ostream& operator <<(ostream& packet, a *t);
  10. friend ostream& operator <<(ostream& packet, a &t);
  11. };
  12.  
  13. // Independent
  14. ostream& operator <<(ostream& packet, const a &t) {
  15. return (packet << t.i);
  16. }
  17.  
  18. // Dependent ones
  19. ostream& operator <<(ostream& packet, const a *t) {
  20. return (packet << (*t));
  21. }
  22.  
  23. ostream& operator <<(ostream& packet, a &t) {
  24. return (packet << const_cast<const a &>(t));
  25. }
  26.  
  27. ostream& operator <<(ostream& packet, a *t) {
  28. return (packet << const_cast<const a *>(t));
  29. }
  30.  
  31. int main() {
  32. a rw(42);
  33. const a ro(35);
  34. cout << rw << endl;
  35. cout << &rw << endl;
  36. cout << ro << endl;
  37. cout << &ro << endl;
  38. return 0;
  39. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
42
42
35
35