1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <iostream>
using namespace std;
// Forward declaration of King required for forward declaration of operator<<.
template<class T>
class King;
// Forward declaration of operator<< required for definition of class King.
template<class T>
ostream& operator<<( ostream& o, const King<T>& king );
template<class T>
class King { T money; public: King( T value ) : money(value) {}
// Guiding declaration
friend ostream& operator<<( ostream&, const King<T>& );
};
template<class T>
ostream& operator<<( ostream& o, const King<T>& king ) {
// operator<< accesses private member king.money.
return o << king.money;
} |