C++,重载输出操作符<<, cout.operator()是什么东东?
本文章已收录于:
分类:
作者同类文章
X
版权声明:本文为博主原创文章,未经博主允许不得转载。
- #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); // 编译通不过...
- }
#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); // 编译通不过...
}
//---
- #include <iostream>
- #include <string>
- using namespace std;
- void main()
- {
- cout.operator <<("csdn"); // 0046E01,是个地址? 为什么不是输出字符串"csdn"?
- }
#include <iostream>
#include <string>
using namespace std;
void main()
{
cout.operator <<("csdn"); // 0046E01,是个地址? 为什么不是输出字符串"csdn"?
}
// --- 答案.....
- #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<< ?
- // 解释:cout<<x1 这种调用格式,我摘录的上篇博文中写的很详细了,不过我没有仔细看,今天看了才清楚.
- /*
- 当运算符重载为类的友元函数时,由于没有隐含的this指针,因此操作数的个数没有变化,所有的操作数都必须通过函数的形参进行传递,函数的参数与操作数自左至右一一对应。
- 调用友元函数运算符的格式如下:
- operator <运算符>(<参数1>,<参数2>) 它等价于 <参数1><运算符><参数2>
- 这里参数1 就是 ostream 对象 cout,参数2就是x1
- */
- cout = operator<<(cout,x1); // 显示调用, cout = cout<<x1; OK
- x1.operator<<(cout,x1); // 错! 这样调用不对. 友元函数不是成员函数么?答不是. 因此不用对象.成员的方式
- /*
- 网上找到一段话,对友元函数的解释,比较好懂:
- 友元函数是可以直接访问类的私有成员的非成员函数。它是定义在类外的普通函数,
- 它不属于任何类,但需要在类的定义中加以声明,声明时只需在友元的名称前加上关键字friend,其格式如下:
- friend 类型 函数名(形式参数);
- 友元函数的声明可以放在类的私有部分,也可以放在公有部分,它们是没有区别的,都说明是该类的一个友元函数。
- 一个函数可以是多个类的友元函数,只需要在各个类中分别声明。
- 友元函数的调用与一般函数的调用方式和原理一致。
- *
- */
- /*
- "<<" 是个什么东东? C++ Prime P6
- 这是个 "输出操作符", 其左边是ostream对象cout,右边是要输出的值
- 它的操作返回输出流本身cout,也就是可以连续写的原因.
- */
- // 既然 cout是 ostream对象,那么可以用 . 的形式 访问ostream的成员函数
- // 但是 operator<< 在本程序中已经重载了,要按照本程序的形参格式传入参数 ?
- operator<<(cout,x1) ; // ok
- ostream z(cout); // ok
- z<<x1; // 等同 cout<<x1
- operator<<(z,x1); // ok
- cout<<"=========================="<<endl;
- z = operator<<(z,x1); //ok
- }
#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<< ?
// 解释:cout<<x1 这种调用格式,我摘录的上篇博文中写的很详细了,不过我没有仔细看,今天看了才清楚.
/*
当运算符重载为类的友元函数时,由于没有隐含的this指针,因此操作数的个数没有变化,所有的操作数都必须通过函数的形参进行传递,函数的参数与操作数自左至右一一对应。
调用友元函数运算符的格式如下:
operator <运算符>(<参数1>,<参数2>) 它等价于 <参数1><运算符><参数2>
这里参数1 就是 ostream 对象 cout,参数2就是x1
*/
cout = operator<<(cout,x1); // 显示调用, cout = cout<<x1; OK
x1.operator<<(cout,x1); // 错! 这样调用不对. 友元函数不是成员函数么?答不是. 因此不用对象.成员的方式
/*
网上找到一段话,对友元函数的解释,比较好懂:
友元函数是可以直接访问类的私有成员的非成员函数。它是定义在类外的普通函数,
它不属于任何类,但需要在类的定义中加以声明,声明时只需在友元的名称前加上关键字friend,其格式如下:
friend 类型 函数名(形式参数);
友元函数的声明可以放在类的私有部分,也可以放在公有部分,它们是没有区别的,都说明是该类的一个友元函数。
一个函数可以是多个类的友元函数,只需要在各个类中分别声明。
友元函数的调用与一般函数的调用方式和原理一致。
*
*/
/*
"<<" 是个什么东东? C++ Prime P6
这是个 "输出操作符", 其左边是ostream对象cout,右边是要输出的值
它的操作返回输出流本身cout,也就是可以连续写的原因.
*/
// 既然 cout是 ostream对象,那么可以用 . 的形式 访问ostream的成员函数
// 但是 operator<< 在本程序中已经重载了,要按照本程序的形参格式传入参数 ?
operator<<(cout,x1) ; // ok
ostream z(cout); // ok
z<<x1; // 等同 cout<<x1
operator<<(z,x1); // ok
cout<<"=========================="<<endl;
z = operator<<(z,x1); //ok
}
//
- #include <iostream>
- #include <string>
- using namespace std;
- void main()
- {
- cout.operator <<("csdn"); // 0046E01,是个地址? 为什么不是输出字符串"csdn"?
- /*
- cout.operator<<("csdn")这样实际上调用的是ostream重载的成员方法operator<<(...),
- 而ostream并没有针对char*类型的成员重载,所以输出整数。应该使用全局的二元重载operator<<来进行字符串输出
- ostream& operator<<(ostream& os, char* str);
- 调用方法有二:方法一:操作符调用: cout<<"csdn";
- 方法二:函数调用:operator<<(cout,"csdn");
- */
- //--
- }
#include <iostream>
#include <string>
using namespace std;
void main()
{
cout.operator <<("csdn"); // 0046E01,是个地址? 为什么不是输出字符串"csdn"?
/*
cout.operator<<("csdn")这样实际上调用的是ostream重载的成员方法operator<<(...),
而ostream并没有针对char*类型的成员重载,所以输出整数。应该使用全局的二元重载operator<<来进行字符串输出
ostream& operator<<(ostream& os, char* str);
调用方法有二:方法一:操作符调用: cout<<"csdn";
方法二:函数调用:operator<<(cout,"csdn");
*/
//--
}
// ---
格式化输出:
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
- }
#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
}
-
顶
- 0
-
踩
- 0
我的同类文章