OJ:重载 << 运算符

时间:2020-12-23 15:38:40

Description

补足程序,使得下面程序输出的结果是:

****100

#include <iostream>
#include <string>
using namespace std;
// Your Code Here
string Print1()
{
return "****";
}
int Print2()
{
return 100;
} int main()
{
cout << Print1 << Print2 << endl;
return 0;
}

Output

****100

解法如下:

#include <iostream>
#include <string>
using namespace std; ostream& operator<<(ostream& out, string (*p)())
{
out << (*p)();
return out;
} ostream& operator<<(ostream& out, int (*p)())
{
out << (*p)();
return out;
} string Print1()
{
return "****";
}
int Print2()
{
return 100;
} int main()
{
cout << Print1 << Print2 << endl;
return 0;
}