Template Class youyuan template function-Zookeeper second-blog
Template class friend Template Function
?
123456789101112131415161718192021222324252627282930313233343536 |
# Include <iostream> # Include <string> Using Namespace STD; Template < Class T> Class Test; Template <
Class T> ostream & operator <(ostream & out, Const Test <t> & OBJ ); Template < Class T> Class Test { Private : Int Num; Public
: Test ( Int N = 0) { Num = N; } Test ( Const Test <t> & copy)
{ Num = copy. Num; } // '<' Followed by '<>' indicates that this function is a function template. Friend Ostream & operator <> (ostream & out, Const Test <t> & OBJ ); }; Template < Class T> ostream & operator <(ostream & out, Const Test <t> & OBJ) { Cout <obj. Num <Endl; Return Out; } Int Main () {
Test < Int > T (2 ); Cout <t <Endl; Getchar (); Return 0; } |
1) It should be noted that the template <class T> should appear exactly where and where.
2) It is important to note that the comment row is added,
1: friend ostream & operator <> ( ostream & out , const test T & OBJ );
This line cannot be written as well,
1: friend template class T ostream & operator <> ( ostream & out , const test T & OBJ );
It cannot be written like this,
1: friend template class T ostream & operator <( ostream & out , const test T & OBJ );
In short, the only correct method isCodeSection. <> It is also essential. In fact, <> there are two meanings: one is, indicating that the friend function is a function template; the other is, the template type parameter used by this template is the type parameter class t of the current template class.
?
123456789101112131415161718192021222324252627282930313233343536 |
# Include <iostream> # Include <string> Using Namespace STD; Template < Class T> Class Test; Template < Class T> ostream & operator <(ostream & out,
Const Test <t> & OBJ ); Template < Class T> Class Test { Private : Int Num; Public :
Test ( Int N = 0) { Num = N; } Test ( Const Test <t> & copy) {
Num = copy. Num; } // '<' Followed by '<>' indicates that this function is a function template. Friend Ostream & operator <> (ostream & out, Const Test <t> & OBJ ); }; Template < Class T> ostream & operator <(ostream & out, Const Test <t> & OBJ) { Cout <obj. Num <Endl; Return Out; } Int Main () { Test <
Int > T (2 ); Cout <t <Endl; Getchar (); Return 0; } |
1) It should be noted that the template <class T> should appear exactly where and where.
2) It is important to note that the comment row is added,
1: friend ostream & operator <> ( ostream & out , const test T & OBJ );
This line cannot be written as well,
1: friend template class T ostream & operator <> ( ostream & out , const test T & OBJ );
It cannot be written like this,
1: friend template class T ostream & operator <( ostream & out , const test T & OBJ );
In short, the only correct method is in the code segment. <> It is also essential. In fact, <> there are two meanings: one is, indicating that the friend function is a function template; the other is, the template type parameter used by this template is the type parameter class t of the current template class.