fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Coor
  5. {
  6. public:
  7. int x, y;
  8. };
  9.  
  10. struct DataAccess
  11. {
  12. virtual int Get(Coor &coor) = 0;
  13. virtual void Set(Coor &coor, int value) = 0;
  14. };
  15.  
  16. struct AccessX : public DataAccess
  17. {
  18. virtual int Get(Coor &coor) { return coor.x; }
  19. virtual void Set(Coor &coor, int value) { coor.x = value; }
  20. };
  21.  
  22. struct AccessY : public DataAccess
  23. {
  24. virtual int Get(Coor &coor) { return coor.y; }
  25. virtual void Set(Coor &coor, int value) { coor.y = value; }
  26. };
  27.  
  28. bool algorithm(Coor &coor, DataAccess &accessor)
  29. {
  30. cout << accessor.Get(coor) << endl;
  31. accessor.Set(coor, 100);
  32. cout << accessor.Get(coor) << endl;
  33. cout << "===============" << endl;
  34.  
  35. return true;
  36. }
  37.  
  38. int main()
  39. {
  40. Coor coor;
  41. coor.x = 5;
  42. coor.y = 10;
  43.  
  44. algorithm(coor, AccessX());
  45. algorithm(coor, AccessY());
  46. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:44: error: invalid initialization of non-const reference of type ‘DataAccess&’ from a temporary of type ‘AccessX’
prog.cpp:28: error: in passing argument 2 of ‘bool algorithm(Coor&, DataAccess&)’
prog.cpp:45: error: invalid initialization of non-const reference of type ‘DataAccess&’ from a temporary of type ‘AccessY’
prog.cpp:28: error: in passing argument 2 of ‘bool algorithm(Coor&, DataAccess&)’
stdout
Standard output is empty