fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Tuna{
  5.  
  6. int x = 50;
  7. Tuna() : x(200){
  8.  
  9. }
  10.  
  11. Tuna &foo(){
  12. return *this; //return type is reference to Tuna class
  13. }
  14.  
  15.  
  16. /****************
  17.  
  18. Returns a new object made by copying the current object
  19.  
  20. *****************/
  21. Tuna bar(){
  22. return *this;
  23. }
  24. void set(int value) { x = value; }
  25.  
  26. };
  27. int main(){
  28.  
  29. // a pointer to class
  30. Tuna tt;
  31. Tuna *foobar = &tt;
  32. foobar->set(20);
  33. cout << foobar->x << endl;
  34.  
  35.  
  36.  
  37. Tuna t;
  38. t.foo().set(1);
  39. cout << t.x << endl;
  40.  
  41.  
  42. system("pause");
  43.  
  44. return 0;
  45.  
  46. }
Success #stdin #stdout #stderr 0s 3296KB
stdin
Standard input is empty
stdout
20
1
stderr
sh: pause: not found