以下代码重载了insertion operator “<<”。
#include <iostream>
#include <algorithm>
using namespace std;
struct YouTubeChannel{
string Name;
int SubscribersCount;
YouTubeChannel(string name, int subscribersCount){
Name = name;
SubscribersCount = subscribersCount;
}
};
ostream& operator<<(ostream& COUT, YouTubeChannel& ytChannel){
COUT << "Name: " << ytChannel.Name << endl;
COUT << "SubscribersCount: " << ytChannel.SubscribersCount << endl;
return COUT;
}
int main(){
YouTubeChannel yt1 = YouTubeChannel("dell", 5000);
YouTubeChannel yt2 = YouTubeChannel("asus", 7000);
cout << yt1 << yt2;
cout << 7;
return 0;
}