#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class A {
friend ostream& operator<<(ostream& os, A& t);//友元函数申明
friend std::string get_string(A& t);
private:
int a;
int b;
public:
A(int a, int b) {
this->a = a;
this->b = b;
}
};
//重载外部定义
ostream& operator<<(ostream& os, A& t) {
os << "a=" << t.a << ",b=" << t.b << endl;
return os;
}
std::string get_string(A& t)
{
return std::to_string(t.a);
}
int main(int argc, char** argv)
{
A t1(1, 2);
cout << 123 << t1;//类内重载,写法很奇怪
//cout << t1 << endl;//类外重载
ofstream ofs;// 2.创建流对象
ofs.open("test.txt", ios::out);
ofs << t1 << 777;
}