fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<class T>
  5. class Number
  6. {
  7. T number;
  8. public:
  9. template <class U> friend class Number;
  10. Number(T num)
  11. {
  12. number=num;
  13. }
  14. template<class U>
  15. Number<T>& operator=(const Number<U>& num)
  16. {
  17. number=static_cast<T>(num.number);
  18. return *this;
  19. }
  20. friend int main();
  21. };
  22.  
  23. int main() {
  24. Number<char> c(10);
  25. Number<int> d(20);
  26. cout << (int)c.number << " " << d.number << endl;
  27. d = c;
  28. cout << (int)c.number << " " << d.number << endl;
  29. return 0;
  30. }
Success #stdin #stdout 0s 4372KB
stdin
Standard input is empty
stdout
10 20
10 10