左移运算符重载
左移運算符重載
作用:可以輸出自定義數據類型
#include <iostream> using namespace std;class Person {friend ostream& operator<<(ostream& out, Person& p);public:Person(int a, int b){this->m_A = a;this->m_B = b;}//成員函數 實現不了 p << cout 不是我們想要的效果//void operator<<(Person& p){//}private:int m_A;int m_B; };//全局函數實現左移重載 //ostream對象只能有一個 ostream& operator<<(ostream& out, Person& p) {out << "a:" << p.m_A << " b:" << p.m_B;return out; }void test() {Person p1(10, 20);cout << p1 << "hello world" << endl; //鏈式編程 }int main() {test();system("pause");return 0; }?
總結
- 上一篇: 初始化列表||类对象作为类成员|| 静态
- 下一篇: 冒泡排序用c语言实现