左移运算符重载
左移運(yùn)算符重載
作用:可以輸出自定義數(shù)據(jù)類型
#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;}//成員函數(shù) 實(shí)現(xiàn)不了 p << cout 不是我們想要的效果//void operator<<(Person& p){//}private:int m_A;int m_B; };//全局函數(shù)實(shí)現(xiàn)左移重載 //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; //鏈?zhǔn)骄幊?}int main() {test();system("pause");return 0; }?
總結(jié)
- 上一篇: 初始化列表||类对象作为类成员|| 静态
- 下一篇: 冒泡排序用c语言实现