重载操作符
在C++中,我们可以重载函数,也可以重载操作符;在C++中大部分的操作符是可以重载的,只有6个不可以;
可以重载的操作符有42个:
不可以重载的操作符有6个:
.
:成员访问运算符.*
,->*
:成员指针访问运算符::
:域运算符sizeof
:长度运算符?:
:条件运算符#:
预处理符号
重载操作符的注意事项:
- 有四个操作符必须定义为类成员函数:
=
、[]
、()
、->
。 - 在声明操作符成员函数时,若该操作符是一个二元函数,则只能声明成友元函数。
- 一般没有特殊规定,重载函数根据习惯定义。
例:
先定义一个类为:
class Rect{
public:
void ShowL() const;
size_t GetLVal();
void SetLVal(size_t L);
void ShowW() const;
size_t GetWVal();
void SetWVal(size_t W);
Rect();
Rect(size_t W,size_t L);
Rect(const Rect& str);
~Rect();
private:
size_t L,W;
};
重载输入输出操作符
//重载声明
friend ostream& operator<<(ostream& out,Rect& rc);
friend istream& operator>>(istream& in ,Rect& rc);
ostream& operator<<(ostream& out ,Rect& rc)
{
out << rc.L << "\t" << rc.W;
return out;
}
istream& operator>>(istream& in ,Rect& rc)
{
in >> rc.L >> rc.W;
return in;
}
重载算术操作符
friend Rect operator+(Rect& rc1,Rect& rc2);
Rect& operator+=(Rect& rc);
friend Rect operator-(Rect& rc1,Rect& rc2);
Rect& operator-=(Rect& rc);
Rect& Rect::operator+=(Rect& rc)
{
this->L += rc.L;
this->W += rc.W;
return *this;
}
Rect operator+(Rect& rc1,Rect& rc2)
{
Rect rc(rc1);
rc += rc2;
return rc;
}
Rect& Rect::operator-=(Rect& rc)
{
if(this->W > rc.W) this->W -= rc.W;
else
this->W = 0;
if(this->L > rc.L) this->L -= rc.L;
else
this->L = 0;
return *this;
}
Rect operator-(Rect& rc1,Rect& rc2)
{
Rect rc(rc1);
rc -= rc2;
return rc;
}
重载逻辑操作符
friend bool operator==(const Rect& rc1,const Rect& rc2);
friend bool operator!=(const Rect& rc1,const Rect& rc2);
friend bool operator>(const Rect& rc1,const Rect& rc2);
friend bool operator<(const Rect& rc1,const Rect& rc2);
friend bool operator>=(const Rect& rc1,const Rect& rc2);
friend bool operator<=(const Rect& rc1,const Rect& rc2);
bool operator==(const Rect& rc1,const Rect& rc2)
{
return (rc1.W == rc2.W && rc1.L == rc2.L ) || (rc1.W*rc1.L == rc2.W*rc2.L);
}
bool operator!=(const Rect& rc1,const Rect& rc2)
{
return !(rc1 == rc2);
}
bool operator>=(const Rect& rc1,const Rect& rc2)
{
return (rc1 == rc2) || (rc1.W*rc1.L > rc2.W*rc2.L);
}
bool operator<=(const Rect& rc1,const Rect& rc2)
{
return (rc1 == rc2) || (rc1.W*rc1.L < rc2.W*rc2.L);
}
bool operator>(const Rect& rc1,const Rect& rc2)
{
return (rc1.W*rc1.L > rc2.W*rc2.L);
}
bool operator<(const Rect& rc1,const Rect& rc2)
{
return (rc1.W*rc1.L < rc2.W*rc2.L);
}
重载赋值操作符
Rect& operator=(const Rect& rc);
Rect& Rect::operator=(const Rect& rc)
{
this->L = rc.L;
this->W = rc.W;
return *this;
}
重载下标操作符
下标操作符在重载的时候,要重载两个版本,一个是不带const
,一个是带const
;例:
const char name[1024] = "121212ds";
char name[1024] = "121212ds";
因此要声明可变成员函数
和常量成员函数
;
size_t operator[](size_t index);
size_t operator[](size_t index) const;
size_t Rect::operator[](size_t index)
{
size_t ret = 0;
switch(index)
{
case 0:
ret = this->L;
break;
case 1:
ret = this->W;
break;
default:
break;
}
return ret;
}
size_t Rect::operator[](size_t index) const
{
size_t ret = 0;
switch(index)
{
case 0:
ret = this->L;
break;
case 1:
ret = this->W;
break;
default:
break;
}
return ret;
}
重载函数调用操作符
void operator()();
void operator()(size_t W,size_t L);
void Rect::operator()()
{
cout << "Rect W:" << this->W << " " << "Rect L:" << this->L << endl;
}
void Rect::operator()(size_t W,size_t L)
{
this->W = W;
this->L = L;
cout << "Rect W:" << this->W << " " << "Rect L:" << this->L << endl;
}
重载转换操作符
operator size_t() const;
Rect::operator size_t() const
{
return this->L*this->W;
}
重载自增自减操作符
Rect& operator++();
Rect const operator++(int);
Rect& operator--();
Rect const operator--(int);
Rect& Rect::operator++()
{
this->L += 1;
this->W += 1;
return *this;
}
Rect const Rect::operator++(int)
{
Rect cop(*this);
++(*this);
return cop;
}
Rect& Rect::operator--()
{
if(this->L - 1 < 0) this->L = 0;
else this->L -= 1;
if(this->W - 1 < 0) this->W = 0;
else this->W -= 1;
return *this;
}
Rect const Rect::operator--(int)
{
Rect cop(*this);
--(*this);
return cop;
}
main
主体:
int main(int argc, char* argv[])
{
Rect rc(120,130);
Rect rc1;
cin >>rc1;
Rect rc2 = rc1 - rc;
cout << rc2 << endl;
rc(); //调用重载函数调用操作符
printf("Hello World!\n");
return 0;
}