032-C++之运算符重载1
1、简介C++可以重载大部分内置的运算符来实现对象之间的操作。
重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。
1. 不能重载的运算符
.:成员访问运算符
.*, ->*:成员指针访问运算符
:::域运算符
sizeof:长度运算符
?::条件运算符
#: 预处理符号
2.运算符重载的限制
重载运算符函数可以对运算符作出新的解释,但原有基本语义不变:
①不改变运算符的优先级
②不改变运算符的结合性
③不改变运算符需要的操作数
④不能创建新的运算符
3.运算符重载两种方式
成员函数重载:
ObjectL.operator op (ObjectR)
其左操作数通过this指针传递,右操作数需要有参数传递,一元操作符时,缺省。
友元函数重载:
operator op (ObjectL, ObjectR)
友元函数重载时其左右操作数都需参数提供。
注意: = () [] ->操作符不能用友元函数重载
cout<< 重载是只能用友元函数
2、+ 的成员函数重载、- 的友元函数重载
class Complex // 复数类
{
public: // 重载操作符需要写到public里
friend Complex operator-(Complex &c1, Complex &c2); // 友元重载的友元声明
Complex operator+(Complex &c2) //c1隐藏于this指针 // 成员函数重载
{
Complex tmp;
tmp.a = this->a + c2.a;
tmp.b = this->b + c2.b;
return tmp; //返回时调用拷贝构造函数
}
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
void print()
{
cout << a << "i+" << b << endl;
}
protected:
private:
int a;
int b;
};
Complex operator-(Complex &c1, Complex &c2) //友元重载的定义
{
Complex c3;
c3.a = c1.a - c2.a;
c3.b = c1.b - c2.b;
return c3;
}
int main()
{
Complex c1(1, 2), c2(3, 4), c3, c4;
c3 = c1+ c2;
c4 = c1 - c2;
c3.print();
c4.print();
system("pause");
return 0;
}
3、前缀--的成员函数重载、前缀++的友元函数重载
class Complex // 复数类
{
public:
friend Complex & operator++(Complex &c2); // 友元重载前缀++声明
Complex operator--() // 成员重载实现前缀--
{
--this->a;
--this->b;
return *this;
}
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
void print()
{
cout << a << "i+" << b << endl;
}
protected:
private:
int a;
int b;
};
Complex & operator++(Complex &c2) // 重载前缀++实现
{
c2.a++;
c2.b++;
return c2;
}
int main()
{
Complex c1(1, 2), c2(3, 4), c3, c4;
c3 = ++c1;
c4 = --c2;
c1.print();
c2.print();
c3.print();
c4.print();
system("pause");
return 0;
}
4、后缀--的成员函数重载、后缀++的友元函数重载
C++为了区分前缀和后缀,规定后缀时需要加上int这个占位参数。
class Complex // 复数类
{
public: // 重载操作符需要写到public里
friend Complex operator++(Complex &c2, int); // 友元重载后缀++声明
Complex operator--(int) // 成员重载实现后缀--
{
Complex tmp = *this;
this->a--;
this->b--;
return tmp;
}
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
void print()
{
cout << a << "i+" << b << endl;
}
protected:
private:
int a;
int b;
};
Complex operator++(Complex &c2, int) // 重载后缀++实现
{
// 先使用c2的属性,再让属性++, 即返回++之前c2的值
Complex tmp;
tmp = c2;
c2.a++;
c2.b++;
return tmp;
}
int main()
{
Complex c1(1, 2), c2(3, 4), c3, c4;
c3 = c1++;
c4 = c2--;
c1.print();
c2.print();
c3.print();
c4.print();
system("pause");
return 0;
}
5、cout<< 的友元重载及它的链式编程
cout.operator<<(Complex &c1), cout不会转化为this指针,及它不能重载为成员函数,只能采用友元重载的方法。
为了使“cout<<a<<b<<endl;”得到实现,必须让重载的返回只能够做左值,让返回值做左值,需要返回引用。
class Complex // 复数类
{
public:
friend ostream& operator<<(ostream &out, Complex &c1); // cou<<的友元重载声明
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
void print()
{
cout << a << "i+" << b << endl;
}
protected:
private:
int a;
int b;
};
// 返回引用,可以做左值
ostream& operator<<(ostream &out, Complex &c1) // cou<<的友元重载实现
{
cout << c1.a << "i+" << c1.b << endl;
return out;
}
int main()
{
Complex c1(1, 2);
cout << c1 << "链式编程测试" << endl;
system("pause");
return 0;
}
页:
[1]