manyfrnd.cpp 659 B

1234567891011121314151617181920212223242526272829303132
  1. // manyfrnd.cpp -- unbound template friend to a template class
  2. #include <iostream>
  3. using std::cout;
  4. using std::endl;
  5. template <typename T>
  6. class ManyFriend
  7. {
  8. private:
  9. T item;
  10. public:
  11. ManyFriend(const T & i) : item(i) {}
  12. template <typename C, typename D> friend void show2(C &, D &);
  13. };
  14. template <typename C, typename D> void show2(C & c, D & d)
  15. {
  16. cout << c.item << ", " << d.item << endl;
  17. }
  18. int main()
  19. {
  20. ManyFriend<int> hfi1(10);
  21. ManyFriend<int> hfi2(20);
  22. ManyFriend<double> hfdb(10.5);
  23. cout << "hfi1, hfi2: ";
  24. show2(hfi1, hfi2);
  25. cout << "hfdb, hfi2: ";
  26. show2(hfdb, hfi2);
  27. // std::cin.get();
  28. return 0;
  29. }