什么是运算符重载
使得自定义类型满足和内置类型相同的逻辑
- 类中:单目不涉及形参,双目形参拿右操作数
- 类外:单目设一个形参,双目设两个形参,第一个接受左操作数,第二个接受右操作数
限制:
- 不能改变运算符的优先级和结合性
- 不能改变原有运算符的用法
- 不能创造运算符
- 不允许有函数的默认值
不能重载的运算符:?: 、 . 、 : 、 :: 、 sizeof 、typeid 、
基础运算符的重载机制
单目运算符
可重载前置++和后置++等
双目运算符
可重载=、<、+、+=、[ ]、<<、 >>等
Cint类重载++、<、[ ]
class Cint
{
public:
Cint(int val):mval(val)
{
}
bool operator<(const Cint& rhs)
{
return mval<rhs.mval;
}
Cint& operator++()
{
++mval;
return *this;
}
Cint operator++(int)
{
return Cint(mval++);
}
int& operator[](int *arr)
{
return arr[mval];
}
private:
int mval;
};
int main()
{
int arr[]={1,32,343,5,45,3,33};
int len=sizeof(arr)/sizeof(arr[0]);
for(Cint i=0;i<len;++i) //i.operator< len
{
cout<<i[arr]<<endl;
}
}
CComplex类重载+、<<、+=、++、
class Ccomplex
{
public:
Ccomplex(int a=0,int b=0):m(a),n(b)
{}
Ccomplex operator+(const Ccomplex& rhs)//com=com1+10;
{
return Ccomplex(m+rhs.m,n+rhs.n);
}
void operator+=(const Ccomplex& rhs) //com1+=com2;
{
m+=rhs.m;
n+=rhs.n;
}
Ccomplex operator++()
{
m++;
n++;
return *this;
}
Ccomplex& operator++(int)
{
return Ccomplex(m++,n++);
}
private:
int m;
int n;
friend Ccomplex operator+(const Ccomplex& lhs,const Ccomplex& rhs);
friend ostream& operator<<(ostream& out,Ccomplex& rhs);
friend istream& operator>>(istream& in,Ccomplex& rhs);
};
// 定义为全局 可实现类似 comp3 = 10 + comp2,左边参数会进行隐式转换
Ccomplex operator+(const Ccomplex& lhs,const Ccomplex& rhs)
{
return Ccomplex(lhs.m+rhs.m,lhs.n+rhs.n);
}
ostream& operator<<(ostream& out,Ccomplex& rhs)
{
out<<rhs.m<<rhs.n<<endl;
return out;
}
istream& operator>>(istream& in,Ccomplex& rhs)
{
in>>rhs.m>>rhs.n;
return in;
}
int main()
{
Ccomplex com(10, 10);
cout << com;
Ccomplex com1(20, 10);
com1 += com;
cout << com1;
Ccomplex com2;
com2 = com + com1;
cout << com2;
Ccomplex com3 = com2++;
cout << com3;
cout << com2;
com3 = ++com2;
cout << com3;
cout << com2;
}
Cstring类重载>、+、[ ]、<<
class Cstring
{
public:
Cstring(char* str=NULL)
{
if(str!=NULL)
{
ptr=new char[strlen(str)+1]();
strcpy(ptr,str);
}
else
{
ptr=new char[1];
ptr='\0';
}
}
~Cstring()
{
if(ptr!=NULL)
{
delete[] ptr;
ptr=NULL;
}
}
Cstring(const Cstring& rhs)
{
ptr=new char[strlen(rhs.ptr)+1]();
strcpy(ptr,rhs.ptr);
}
Cstring& operator=(const Cstring& rhs)
{
if(this!=&rhs)
{
delete[] ptr;
ptr=new char[strlen(rhs.ptr)+1]();
strcpy(ptr,rhs.ptr);
}
return *this;
}
bool operator<(const Cstring& rhs)
{
return strcmp(ptr,rhs.ptr)<0;
}
bool operator>(const Cstring& rhs)
{
return strcmp(ptr,rhs.ptr)>0;
}
char& operator[](int index)
{
return ptr[index];
}
private:
char *ptr;
friend Cstring operator+(const Cstring& rhs,const Cstring& lhs);
friend ostream& operator<<(ostream &out, const Cstring &str);
friend istream& operator>>(istream &in, Cstring &str);
};
Cstring operator+(const Cstring& rhs,const Cstring& lhs)
{
char *newstr=new char[strlen(rhs.ptr)+strlen(lhs.ptr)+1]();
strcpy(newstr,rhs.ptr);
strcat(newstr,lhs.ptr);
Cstring tmp(newstr);
delete[] newstr;
return tmp;
}
ostream& operator<<(ostream &out, const Cstring &str)
{
out<<str.ptr<<endl;
return out;
}
istream& operator>>(istream &in, Cstring &str)
{
char tmp[100]={0};
in>>tmp;
str.ptr=new char[strlen(tmp)+1];
strcpy(str.ptr,tmp);
return in;
}
int main()
{
Cstring str1 = "aaa";
Cstring str2 = str1;
Cstring str3;
str3 = str1;
Cstring str4 = str1 + str3;
str4 = str1 + "bbb";
str4 = "Ccccc" + str1;
cout << str4 << endl;
if (str4 > str1)
{
cout << "str4 > str1" << endl;
}
else
{
cout << "str4 < str1" << endl;
}
for (int i = 0; i < 3; i++)
{
//str4.operator[](int index)
cout << str4[i]<< " ";
}
cout << endl;
Cstring test;
cin >> test;
cout << test << endl;;
Cstring test1 = "AAAA";
cout << test1 << endl;
return 0;
}