C++、リロード出力オペレータ<
#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"?
}
//---答えは.....
http://topic.csdn.net/u/20121006/15/eba3d438-2f71-4b39-b8d5-f6c4a94e3d1b.html?seed=131699817&r=79821898#r_79821898
#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");
*/
//--
}
//---
フォーマット出力: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
}