Template class friend template function template class friend Template Function

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.