Implementation of the input/output stream overload function of the template class in C ++

Source: Internet
Author: User
(Conversion) "<" and ">" overload of operators in the C ++ template class

17:05:31 | category:

It and program | Tag:
| Large font size, medium/small subscription

Input stream "" in the template class and reload of the output stream ". If you declare in the class using youyuan and implement it outside the class, an error will be reported during connection, however, we can use the following three methods to overload the output stream "<" and "input stream>.

1. Write the output stream "<" and "input stream>" overload implementation in the class

# Include "stdafx. H"
# Include <iostream>
Using namespace STD;

Template <class T>
Class Test
{
Public:
Test (const T & T): Data (t ){}
//---------------------------------------------
Friend ostream & operator <(ostream & out, test <t> & T) // output stream overload declaration and implementation
{
Return out <"data is" <t. Data;
}//--------------------------------------------
Friend istream & operator> (istream & in, test <t> & T) // input stream overload declaration and implementation
{
Return in> T. Data;
}//---------------------------------------------
PRIVATE:
T data;
};//-----------------------------------------------------------------

Int main ()
{
Test <int> B (3 );
Cout <B <'\ n ';
Cin> B;
Cout <B <'\ n ';
Return 0;
}

Why can't I declare the input/output stream overload in the class and implement it outside the class ?? Because templates are special, if they are overloaded outside the template class:

Template <class T>
Ostream & operator <(ostream & out, test <t> & T)
{
Return out <"data is" <t. Data;
}//--------------------------------------------

The above is exactly the definition of the function template, and we know that the operator overload function is not a member function of the class, therefore, a new function template is defined here (different from the friend ostream & operator <(ostream & out, test <t> & T) in the class )). However, if the template <class T> is removed, the test parameter in the function does not know what type it is. Therefore, it cannot be declared in the template class, and operators outside the class are overloaded.

2. Since an out-of-class implementation is equivalent to redefining a function template, as long as it does not use private members of the class, therefore, the overloaded function template only uses the public member functions of the class to perform operations on the private member of the class. In this way, you do not have to declare it as a friend in the class and directly reload it out of the class.

# Include "stdafx. H"
# Include <iostream>
Using namespace STD;

Template <class T>
Class Test
{
Public:
Test (const T & T): Data (t ){}
T getdata () const {return data ;}
Void setdata (T & item) {DATA = item ;}
PRIVATE:
T data;
};//-----------------------------------------------------------------
Template <class T>
Ostream & operator <(ostream & out, test <t> & T)
{
Return out <"data is" <t. getdata ();
}//--------------------------------------------
Template <class T>
Istream & operator> (istream & in, test <t> & T)
{
T item;
In> item;
T. setdata (item );
Return in;
}//---------------------------------------------
Int main ()
{
Test <int> B (3 );
Cout <B <'\ n ';
Cin> B;
Cout <B <'\ n ';
Return 0;
}

Iii. Use Transition Functions

# Include "stdafx. H"
# Include <iostream>
Using namespace STD;

Template <class T>
Class Test
{
Public:
Test (const T & T): Data (t ){}
//---------------------------------------------
Template <class chart, class chartraits>
Basic_ostream <chart, chartraits> & output (basic_ostream <chart, chartraits> & out) const // output stream transition function
{
Return out <"data is" <data;
}//--------------------------------------------
Template <class chart, class chartraits>
Basic_istream <chart, chartraits> & input (basic_istream <chart, chartraits> & in) // input stream transition function
{
Return in> data;
}//---------------------------------------------
PRIVATE:
T data;
};//-----------------------------------------------------------------
Template <class T, class chart, class chartraits>
Basic_ostream <chart, chartraits> & operator <(basic_ostream <chart, chartraits> & out, const test <t> & T) // output stream overload
{
Return T. Output (out );
}//------------------------------------------------------------------
Template <class T, class chart, class chartraits>
Basic_istream <chart, chartraits> & operator> (basic_istream <chart, chartraits> & in, test <t> & T) // input stream overload

{
Return T. Input (in );
}//------------------------------------------------------------------
Int main ()
{
Test <int> B (4 );
Cout <B <'\ n ';
Cin> B;
Cout <B <'\ n ';
Return 0;
}

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.