1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- //4.5.2 左移运算符重载
- //作用 : 可以输出自定义数据类型
- #if(0)
- #include <iostream>
- class Person
- {
- public:
- Person(int a , int b)
- {
- m_A = a;
- m_B = b;
- }
- friend std::ostream& operator<<(std::ostream& cout, Person& p);
- //利用成员函数重载 左移运算符 p.operator<<(cout) 简化版本 p << cout
- //不会利用成员函数重载<<运算符,因为无法实现 cout 在左侧
- //void operator<< (cout)
- //{
- //}
- private:
- int m_A;
- int m_B;
- };
- //只能利用全局函数重载左移运算符
- std::ostream& operator<<(std::ostream &cout , Person &p) //本质 operator<< (cout,p) 简化 cout << p
- {
- std::cout << "m_A = " << p.m_A << " m_B = " << p.m_B;
- return cout;
- }
- void test01()
- {
- Person p(10,10);
- //p.m_A = 10;
- //p.m_B = 10;
- std::cout << p << std::endl;
- }
- int main()
- {
- test01();
- system("pause");
- return 0;
- }
- #endif
|