fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <typename T> class Coor;
  6. template <typename T> T getX(const Coor<T>& c);
  7. template <typename T> T getY(const Coor<T>& c);
  8.  
  9. template <typename T>
  10. class Coor {
  11. friend T getX<>(const Coor<T>& c);
  12. friend T getY<>(const Coor<T>& c);
  13. private:
  14. T x;
  15. T y;
  16. public:
  17. Coor(const T& a, const T& b) : x(a), y(b) {}
  18. };
  19.  
  20. template <typename T>
  21. T getX(const Coor<T>& c) {
  22. return c.x;
  23. }
  24.  
  25. template <typename T>
  26. T getY(const Coor<T>& c) {
  27. return c.y;
  28. }
  29.  
  30. int main() {
  31. Coor<int> coor(0, 1);
  32. Coor<double> dcoor(0.123, 12.345);
  33. cout << getX(coor) << ", " << getY(coor) << endl;
  34. cout << getX(dcoor) << ", " << getY(dcoor) << endl;
  35. return 0;
  36. }
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
0, 1
0.123, 12.345