>>,<<,<,<=,>,>=的重载 一般分为两类
1.声明为友元函数,用法为 cout << p
2.声明为成员函数,用法为 p << count
#include <iostream>
using namespace std;
class Point3d
{
private:
double x;
double y;
double z;
public:
Point3d(double tx, double ty, double tz);
/// 重载为友元函数时,表示为其友元类重载运算符,其第一个参数为友元类对象的引用,
friend ostream&
operator <<(ostream& os, const Point3d &pt);
friend istream&
operator >>(istream& os, Point3d &pt);
inline ostream&
operator <<(ostream& os){
os<<"inline method, "<<"x: "<<x<<", y: "<<y<<", z: "<<z;
return os;
}
inline istream&
operator >> ( istream &os){
os >> x >> y >> z;
std::cout << "inline "<<std::endl;
return os;
}
};
ostream&
operator <<(ostream& os, const Point3d &pt){
os<<"friend method, "<<"x: "<<pt.x<<", y: "<<pt.y<<", z: "<<pt.z;
return os;
}
istream&
operator >>(istream& os, Point3d &pt){
os >> pt.x >> pt.y >> pt.z;
std::cout << "friend"<< std::endl;
return os;
}
Point3d::Point3d(double tx, double ty, double tz){
x=tx;
y=ty;
z=tz;
}
int main(void){
Point3d a(1,2,3);
cout<<a<<endl;
a<<cout<<endl;
cin>>a;
a>>cin;
}
LChar的重载就能满足 'A'<= object && object <= 'Z'
其中 'A<= object 是调用的友元函数,视为char重载了对LChar的小于等于
object<='Z'是调用的成员函数,视为LChar 重载了对char的小于等于
struct LChar {
LChar()
:ch_(0u)
{}
LChar(char ch)
:ch_( uint32_t(unsigned char(ch)))
{}
LChar(uint32_t ch)
:ch_(ch)
{}
LChar(LChar const &ch)
:ch_(ch.ch_)
{}
LChar& operator=(char ch) {
ch_ = uint32_t(unsigned char(ch));
return *this;
}
LChar& operator=(uint32_t ch) {
ch_ = ch;
return *this;
}
LChar& operator=(LChar const &ch)
{
ch_ = ch.ch_;
return *this;
}
bool operator ==(LChar const & ch) const {
return ch_ == ch.ch_;
}
bool operator != (LChar const & ch) const {
return !(*this == ch);
}
bool operator<(LChar const &ch) const {
return ch_ < ch.ch_;
}
bool operator<=(LChar const & ch) const {
return ch_ <= ch.ch_;
}
bool operator>(LChar const &ch) const {
return ch_ > ch.ch_;
}
bool operator>=(LChar const &ch) const {
return ch_ >= ch.ch_;
}
bool operator==(char ch) const {
return *this == LChar(ch);
}
bool operator != (char ch) const {
return *this != LChar(ch);
}
bool operator<(char ch) const {
return *this < LChar(ch);
}
bool operator <= (char ch) const {
return *this <= LChar(ch);
}
bool operator > (char ch) const {
return *this > LChar(ch);
}
bool operator >= (char ch) const {
return *this >= LChar(ch);
}
bool operator==(uint32_t ch) const {
return *this == LChar(ch);
}
bool operator != (uint32_t ch) const {
return *this != LChar(ch);
}
bool operator<(uint32_t ch) const {
return *this < LChar(ch);
}
bool operator <= (uint32_t ch) const {
return *this <= LChar(ch);
}
bool operator > (uint32_t ch) const {
return *this > LChar(ch);
}
bool operator >= (uint32_t ch) const {
return *this >= LChar(ch);
}
friend bool operator!=( char ch, LChar const &lc);
friend bool operator==(char ch, LChar const &lc);
friend bool operator<(char ch, LChar const & lc);
friend bool operator<=(char ch , LChar const & lc);
friend bool operator>(char ch, LChar const & lc);
friend bool operator>=(char ch, LChar const & lc);
friend bool operator!=(uint32_t ch, LChar const & lc);
friend bool operator==(uint32_t ch, LChar const & lc);
friend bool operator<(uint32_t ch, LChar const & lc);
friend bool operator<=(uint32_t ch, LChar const & lc);
friend bool operator>(uint32_t ch, LChar const & lc);
friend bool operator>=(uint32_t ch, LChar const & lc);
friend std::ostream& operator<<(std::ostream & os, LChar const & ch);
friend std::istream& operator>>(std::istream& is, LChar & ch);
private:
uint32_t ch_;
};
inline bool operator!=( char ch, LChar const &lc) {
return LChar(ch) == lc;
}
inline bool operator==(char ch, LChar const &lc) {
return LChar(ch) == lc;
}
inline bool operator<(char ch, LChar const &lc) {
return LChar(ch) < lc;
}
inline bool operator<=(char ch, LChar const &lc) {
return LChar(ch) <= lc;
}
inline bool operator>(char ch, LChar const &lc) {
return LChar(ch) > lc;
}
inline bool operator>=(char ch, LChar const &lc) {
return LChar(ch) >= lc;
}
inline bool operator!=(uint32_t ch, LChar const &lc) {
return LChar(ch) != lc;
}
inline bool operator==( uint32_t ch, LChar const &lc) {
return LChar(ch) == lc;
}
inline bool operator<(uint32_t ch, LChar const &lc) {
return LChar(ch) < lc;
}
inline bool operator<=(uint32_t ch, LChar const &lc) {
return LChar(ch) <= lc;
}
inline bool operator>(uint32_t ch, LChar const &lc) {
return LChar(ch) > lc;
}
inline bool operator>=(uint32_t ch, LChar const &lc) {
return LChar(ch) >= lc;
}
inline std::ostream& operator<<(std::ostream & os, LChar const & ch) {
os << ch.ch_;
return os;
}
inline std::istream& operator>>(std::istream& is, LChar & ch) {
is >> ch.ch_;
return is;
}
从c++重载运算符来看: z=x op y 可视为 z = x operator op ( y) 或者 z = oprator op (x,y)