输入输出函数重载
1-输入输出函数一定不能为成员函数
//输入和输出的重载一定不能是成员函数,
#include <iostream>
#include <cstring>
using namespace std;
class out
{
private:
int age;
string name;
public:
void operator<<(const out &m_out);
out(int age, string name);
};
out::out(int age, string name) : age(age), name(name){};
void out::operator<<(const out &m_out)
{
cout << this->age << "\t" << this->name << endl;
cout << m_out.age << "\t" << m_out.name << endl;
};
int main()
{
out out1(11, "aa");
out out2(22, "bb");
out1 << out2; //这样我们虽然能实现直接打印对象,但这样语义并不明确,而且没有用到cout 但是如果加上cout ,就无法匹配相应的重载函数,所以对<<的重载,必须有两个参数,左边是cout对象,右边是传入对象
}
2-理解输入输出流
#include <iostream>
#include <cstring>
using namespace std;
class out
{
private:
int age;
string name;
public:
friend void operator<<(ostream &m_cout,const out &m_out);
out(int age, string name);
};
out::out(int age, string name) : age(age), name(name){};
void operator<<( ostream &m_cout,const out &m_out)
{
cout << m_out.age << "\t" << m_out.name << endl;
};
int main()
{
out out1(11, "aa");
out out2(22, "bb");
cout<<out1;
cout<<"***************************"<<endl;
cout<<out2;
//但无法实现连续打印,《例如不能实现cout<<out1<<out2; 》明白这个问题,要先知道输出流的顺序
}
左移运算符是连续返回左值给cout, cout<<1<<2<<3; 相当于 先进行cout<<1, 把1返回给cout,即(cout<<1)返回cout,再进行(cout<<1)<<2,也就是cout<<2,
最后进行((cout<<1)<<2)<<3.所以左移运算符的返回值是cout. 所以重载函数的返回值对应ostream
其次: 2-而且默认重载的左移运算符的左边是非const,所以我们再次重载的时候,也要保证<<左边是非const. m_cout要保证为非const,如果定义成全局函数则不能使用const限定函数为常函数
3-最终的版本
#include <iostream>
#include <cstring>
using namespace std;
class out
{
private:
int age;
string name;
public:
friend ostream& operator<<(ostream &m_cout,const out &m_out);
out(int age, string name);
};
out::out(int age, string name) : age(age), name(name){};
ostream& operator<<( ostream &m_cout,const out &m_out)
{
m_cout << m_out.age << "\t" << m_out.name ;
return m_cout;
};
int main()
{
out out1(11, "aa");
out out2(22, "bb");
cout<<out1<<endl;
cout<<"***************************"<<endl;
cout<<out2<<"\t"<<out1<<endl;
//但无法实现连续打印,(就像cout<<out1<<out2;)明白这个问题,要先知道输出流的顺序
}
4-之前写的博客
#include <iostream>
using namespace std;
class point
{
int x;
int y;
friend ostream &operator<<(ostream &cout, const point &m_p); //定义为·私有也行
friend istream &operator>>(istream &m_cin, point &m_p);
public:
point(int m_x = 0, int m_y = 0) : x(m_x), y(m_y){};
//friend ostream &operator<<(ostream &cout, const point &m_p);//友元函数不属于类,所以不受所在类的声明区域public ,private,protected的影响,定义在类的任何区域都可以
};
// 1-左移运算符是连续返回左值给cout, cout<<1<<2<<3; 相当于 先进行cout<<1, 把1返回给cout,即(cout<<1)返回cout,再进行(cout<<1)<<2,也就是cout<<2,
// 最后进行((cout<<1)<<2)<<3.所以左移运算符的返回值是cout.
// 2-而且默认重载的左移运算符的左边是非const,所以我们再次重载的时候,也要保证<<左边是非const.
ostream &operator<<(ostream &m_cout, const point &m_p) //m_cout要保证为非const,如果定义成全局函数则不能使用const限定函数为常函数,那自然返回值也不能是const类型,因为返回的常对象在返回给cout时,要保证<<左边不是const
{
m_cout << m_p.x << m_p.y;
return m_cout;
}
istream &operator>>(istream &m_cin, point &m_p) //因为输入数据的操作就是在改变传入参数的过程,所以不要加const修饰point&m_p
{
m_cin >> m_p.x >> m_p.y;
return m_cin;
}
int main()
{
point p1(3, 4);
point p2(1, 3);
cout << p1 << p2 << endl; //如果左移运算符返回值是const,就会报错
point p3;
point p4;
cin >> p3 >> p4;
cout << p3 << p4 << endl;
}