Ci sono due soluzioni per fare quello che chiedi:
il primo ( e più usato ) è:
codice:
template <typename T>
class myClass
{
// Overloading dell'operatore di output <<
friend ostream & operator<<( ostream &output, const myClass &right ) {
output<< right.membro_privato;
/* altro */
return output;
}
// Overloading dell'operatore di input >>
friend istream & operator>>( istream &input, myClass &right ) {
input >> right.membro_privato;
/* altro */
return input ;
}
};
il secondo più lungo ( e usato meno):
codice:
template <typename T>
class myClass;
template <typename T>
ostream & operator<<( ostream &output, const myClass<T> &right );
template <typename T>
istream & operator>>( istream &input, myClass<T> &right );
template <typename T>
class myClass
{
// Overloading dell'operatore di output <<
friend ostream & operator<< <>( ostream &output, const myClass &right );
// Overloading dell'operatore di input >>
friend istream & operator>> <>( istream &input, myClass &right );
};
template <typename T>
ostream & operator<<( ostream &output, const myClass<T> &right ) {
output<< right.membro_privato;
/* altro */
return output;
}
// Overloading dell'operatore di input >>
template <typename T>
istream & operator>>( istream &input, myClass<T> &right ) {
input >> right.membro_privato;
/* altro */
return input ;
}
Qual'è la forma corretta?
Tutte e due, anche se la seconda è pleonastica e non si usa.