运算符重载规则如下:
①、 C++中的运算符除了少数几个之外,全部可以重载,而且只能重载C++中已有的运算符。
②、 重载之后运算符的优先级和结合性都不会改变。
③、 运算符重载是针对新类型数据的实际需要,对原有运算符进行适当的改造。一般来说,重载的功能应当与原有功能相类似,不能改变原运算符的操作对象个数,同时至少要有一个操作对象是自定义类型。
不能重载的运算符只有五个:
它们是:成员运算符“.”、指针运算符“*”、作用域运算符“::”、“sizeof”、条件运算符“?:”。
运算符重载形式有两种,重载为类的成员函数和重载为类的友元函数。
运算符重载为类的成员函数的一般语法形式为:
函数类型 operator 运算符(形参表)
{
函数体;
}
运算符重载为类的友元函数的一般语法形式为:
friend 函数类型 operator 运算符(形参表)
{
函数体;
}
其中,函数类型就是运算结果类型;operator是定义运算符重载函数的关键字;运算符是重载的运算符名称。
参考代码如下:
class String
{
public:
//构造
String(char* pstring) //构造
{
pstr = new char[strlen(pstring) + 1]();
strcpy(pstr, pstring);
}
//类内是this_call的调用约定,成员方法隐藏this指针,指向一般为左操作数,所以只用传右操作数即可
String(const String& rhs)
{
pstr = new char[strlen(rhs.pstr) + 1]();
strcpy(pstr, rhs.pstr);
}
类内=(赋值)的重载,左右操作数类型均为String,类内只用传右操作数即可
String& operator=(const String& rhs)
{
if (this != &rhs)
{
delete[] pstr;
pstr = new char[strlen(rhs.pstr) + 1]();
strcpy(pstr, rhs.pstr);
}
return *this;
}
//析构
~String()
{
delete[] pstr;
pstr = NULL;
}
类内实现+的重载,传入右操作数类型为char*(可省略:char*可隐式生成临时对象,调用左右操作数都为string类的重载函数)
//const String operator+(char* prhs)
//{
// char* pnewstr = new char[strlen(pstr) + strlen(prhs) + 1]();
// strcat(pnewstr, pstr);
// strcat(pnewstr, prhs);
// String tmp(pnewstr);
// delete[] pnewstr;
// return tmp;
//}
String str2 = str1 + “world”;//“helloworld” +是自左向右,可根据str1.推演str2的类型
String str3 = “hi” + str1;//“hihello”;错误,不能根据右边类型推演左边类型,只能调用左操作符为char*,右操作符为string的函数,或者两个类型都是string 的函数
类内==的重载,左右操作数类型均为String,类内只用传右操作数即可
bool operator==(const String& rhs)
{
return strcmp(pstr, rhs.pstr) == 0;
}
if (str1 == str2)
{
std::cout << str1[0] << std::endl;// int p[10] ==> p int* p[1]
}
类内[]的重载,左操作数类型为String,右操作数类型为int,类内只用传右操作数即可
char& operator[](int index)
{
return pstr[index];
}
std::cout << str1[0] << std::endl;// int p[10] ==> p int* p[1]
类内<的重载,左操作数类型为std::cout,右操作数类型为String,类内只用传右操作数即可
bool operator<(const String& rhs)
{
return strcmp(pstr, rhs.pstr) < 0;
}
if (str2 < str2)
{
std::cout << str2 << std::endl;
}
private:
char* pstr;
//类外不能访问类内的私有成员变量,设置友元函数
friend const String operator+(char*, const String&);
friend const String operator+(const String&, const String&);
friend std::ostream& operator <<(std::ostream&, const String&);
};
//类外是cd_call的调用约定,无this指针,需传两个对象
//类外实现+的重载,左操作数类型为char*,右操作数为String(可省略:char*可先调用构造生成临时对象,后调用左右操作数都为string类的重载函数)
//const String operator+(char* plhs, const String& rhs)
//{
// char* pnewstr = new char[strlen(plhs) + strlen(rhs.pstr) + 1]();
// strcat(pnewstr, plhs);
// strcat(pnewstr, rhs.pstr);
// String tmp(pnewstr);
// delete[] pnewstr;
// return tmp;
//}
String str3 = “hi” + str1;//“hihello”;
类外实现+的重载,左右操作数类型均为String
const String operator+(const String& lhs, const String& rhs)
{
char* pnewstr = new char[strlen(lhs.pstr) + strlen(rhs.pstr) + 1]();
strcat(pnewstr, lhs.pstr);
strcat(pnewstr, rhs.pstr);
String tmp(pnewstr);
delete[] pnewstr;
return tmp;
}
String str4 = str1 + str2; //“hellohelloworld”
类外实现<<的重载,左操作数为std::ostream的类型,右操作数为String类型
std::ostream& operator <<(std::ostream& out, const String& rhs)
{
out << rhs.pstr;
return out;
}