作用:可以输出自定义数据类型

    1. //左移运算符重载
    2. class Person
    3. {
    4. friend ostream& operator<<(ostream& out, Person& p);
    5. public:
    6. //利用成员函数重载左移运算符 p.operator<<(cout) 简化版本为 p << cout
    7. //一般不用利用成员函数重载<<运算符 因为无法实现 cout在左侧
    8. //void operator<<(cout)
    9. //{
    10. // cout<<
    11. //}
    12. Person(int a, int b):m_A(a), m_B(b){}
    13. private:
    14. int m_A;
    15. int m_B;
    16. };
    17. //利用全局函数重载左移运算符
    18. ostream& operator<<(ostream &out, Person& p) //本质 operator<<(cout, p) 简化 cout << p
    19. {
    20. out << "m_A = " << p.m_A << " m_B = " << p.m_B;
    21. return out;
    22. }
    23. void test01()
    24. {
    25. Person p1(10, 10);
    26. //p1.m_A = 10;
    27. //p1.m_B = 10;
    28. cout << p1 << endl;
    29. }
    30. int main()
    31. {
    32. test01();
    33. system("pause");
    34. return 0;
    35. }