请教:C++重载输出操作符<<的问题, cout.operator()是什么东东?

sergery 2012-10-06 03:23:43

问题来自我的一篇 学习博客 :
http://blog.csdn.net/sergery/article/details/8043203


问题1:cout.operator <<() 该怎么用?


#include <iostream>
#include <string>
using namespace std;

void main()
{
cout.operator <<("csdn"); // 0046E01,是个地址? 为什么不是输出字符串"csdn"?
}




问题2: 下面的代码,重载了"<<", 那么 cout<<x1 能实现输出的具体调用形式是什么?

#include <iostream>
#include <string>
using namespace std;

class Student
{
public:
// 存
void setname(string s){ name = s;}
void setage(int y){age = y; }
void setaddress(string add){address = add;}
// 取
string getname(){return name;}
int getage(){return age;}
string getaddress(){return address;}

Student(string name="",int age=0,string address="")
{
this->name = name; this->age = age; this->address = address;
}
~Student(){}


//重载 运算符<< : 把 "operator<<" 看作是函数名, 返回类型是 ostream类型的对象引用
friend ostream& operator<< (ostream &os,Student &st)
{
os<<st.name<<"------"<<st.age<<"------"<<st.address<<endl;
return os;
}
protected:
private:
string name;
int age;
string address;
};

void main()
{
Student x1("刘莉莉",22,"东风路369号");
cout<<x1;
//cout<<x1 ---结果OK,问题是这个写法很怪,是谁在调用那个重载函数?,x1显然是个参数,那是cout调用的 operator<< ?


/*
"<<" 是个什么东东? C++ Prime P6
这是个 "输出操作符", 其左边是ostream对象cout,右边是要输出的值
它的操作返回输出流本身cout,也就是可以连续写的原因.

*/

// 既然 cout是 ostream对象,那么可以用 . 的形式 访问ostream的成员函数
// 但是 operator<< 在本程序中已经重载了,要按照本程序的形参格式传入参数 ?
ostream z;
cout.operator<<(z,x1); // 编译通不过...
}
...全文
570 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
sergery 2012-10-06
  • 打赏
  • 举报
回复
谢谢 mujiok2003 的举例和解释.

结贴了.

2位给出解释的csdner 分配: 30 + 10 吧. 谢谢各位!
mujiok2003 2012-10-06
  • 打赏
  • 举报
回复
格式化输出:
1. 成员函数: ostream::operator <<, 支持整数(不包括字符类型),浮点数,bool, const void*等。
2。全局函数: operator<<(ostream& os, T const&), 支持字符,字符串和自定义类型

理解下面的例子就明白怎么回事了

#include <iostream>
#include <iomanip>
#include <sstream>
int main()
{
std::istringstream input(" \"Some text.\" ");
volatile int n = 42;
double f = 3.14;
bool b = true;;
std::cout << n // int overload
<< ' ' // non-member overload
<< std::boolalpha << b // bool overload
<< " " // non-member overload
<< std::fixed << f // double overload
<< input.rdbuf() // streambuf overload
<< &n // bool overload
<< std::endl; // function overload
}
sergery 2012-10-06
  • 打赏
  • 举报
回复

operator(z,x1) 在执行的时候是谁在调用这个函数"operator << "?
sergery 2012-10-06
  • 打赏
  • 举报
回复
"再次强调,cout.operator<<(...)实际上调用的是cout(即ostream)重载的成员方法,这里是操作符方法,而我们无法扩展ostream的实现,让它支持student的输出。但是可以通过定义全局重载的operator<<来输出。但是要使用全局的操作符,就不能使用cout.operator这种方法来调用"

----

还有点疑问是 :
friend ostream& operator<< (ostream &os,Student &st)
我还是C的思维习惯 : 把 "operator<<" 看作是函数名,
那么:
operator(z,x1) 在执行的时候是谁在调用这个函数"operator"?
sergery 2012-10-06
  • 打赏
  • 举报
回复
谢谢mars同学!
按照你说的方法,测试通过.
给你满分!
谢谢!

operator<<(cout,x1) ; // ok

ostream z(cout); // ok
z<<x1;
operator<<(z,x1); // ok
virtualxmars 2012-10-06
  • 打赏
  • 举报
回复
至于上面的operator<<为什么要返回ostream& 的返回值,是为了进行连续的输出,如:
cout<<"abc"<<"def";

上面的代码包含了两个 operator<< 的调用,首先输出abc,然后返回 cout的引用,接着再用cout输出def
virtualxmars 2012-10-06
  • 打赏
  • 举报
回复
打个比方吧,比如你要重载加号:+。因为必须提供加数和被加数,才能完成加法操作,所以必须提供两个参数。
而且,加法操作会返回一个得数——和。所以可将操作符重载为:
int operator+(int a, int b);

那么,当你写下:
int sum;
sum = 1 + 2;
这样的代码时,编译器会暗地里将它看到的操作符全部转换成方法调用!即上面定义的方法。

同理,如果重载operator<<,则当你写下:
cout<<"abc";
则,编译器也会寻找对应的 operator<<(ostream&, char*) 来执行。

建议买一本《effective c++》、《more effective c++》参考一下,里面有详细的解释
virtualxmars 2012-10-06
  • 打赏
  • 举报
回复
第一个问题:
cout.operator<<("csdn")这样实际上调用的是ostream重载的成员方法operator<<(...),而ostream并没有针对char*类型的成员重载,所以输出整数。应该使用全局的二元重载operator<<来进行字符串输出
ostream& operator<<(ostream& os, char* str);
调用方法有二:方法一:操作符调用: cout<<"csdn";
方法二:函数调用:operator<<(cout,"csdn");

第二个问题:
和第一个问题一样,应该使用z<<x1,或者 operator<<(z,x1) 来调用即可

再次强调,cout.operator<<(...)实际上调用的是cout(即ostream)重载的成员方法,这里是操作符方法,而我们无法扩展ostream的实现,让它支持student的输出。但是可以通过定义全局重载的operator<<来输出。但是要使用全局的操作符,就不能使用cout.operator这种方法来调用
卖萌de猫 2012-10-06
  • 打赏
  • 举报
回复
这是C++的规矩,你要用它就必须遵守它的用法。第一个我也不懂。

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧