操作符重载:
操作符重载为操作符提供不同的语义
1.通过operator关键字可以利用函数扩展操作符,operator的本质是通过函数重载实现操作符重载
例1::扩展“+”,使他可以支持复数相加
#include <cstdlib>
#include <iostream>
using namespace std;
struct Complex
{
int a;
int b;
};
Complex operator+(const Complex& c1, const Complex& c2)
{
Complex ret;
ret.a = c1.a + c2.a;
ret.b = c1.b + c2.b;
return ret;
}
int main(int argc, char *argv[])
{
Complex c1 = {1, 2};
Complex c2 = {3, 4};
Complex c3 = c1+c2;
cout<<"c3.a = "<<c3.a<<endl;
cout<<"c3.b = "<<c3.b<<endl;
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
2.用operator关键字扩展的操作符可以用于类:
private声明使得类的成员不能被外界访问,但是通过friend关键字可以例外的开放权限
例二:
#include <cstdlib>
#include <iostream>
using namespace std;
class Complex
{
int a;
int b;
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
friend Complex operator+ (const Complex& c1, const Complex& c2);
};
Complex operator+ (const Complex& c1, const Complex& c2)
{
Complex ret;
ret.a = c1.a + c2.a;
ret.b = c1.b + c2.b;
return ret;
}
int main(int argc, char *argv[])
{
Complex c1(1, 2);
Complex c2(3, 4);
Complex c3 = c1 + c2;
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
例三:重载“<<”
#include <cstdlib>
#include <iostream>
using namespace std;
class Complex
{
int a;
int b;
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
friend Complex operator+ (const Complex& c1, const Complex& c2);
friend ostream& operator<< (ostream& out, const Complex& c);
};
ostream& operator<< (ostream& out, const Complex& c)
{
out<<c.a<<" + "<<c.b<<"i";
return out;
}
Complex operator+ (const Complex& c1, const Complex& c2)
{
Complex ret;
ret.a = c1.a + c2.a;
ret.b = c1.b + c2.b;
return ret;
}
int main(int argc, char *argv[])
{
Complex c1(1, 2);
Complex c2(3, 4);
Complex c3 = c1 + c2;
cout<<c1<<endl;
cout<<c2<<endl;
cout<<c3<<endl;
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
3.用成员函数重载的操作符,比全局操作符重载函数少一个参数,即左操作数,不需要使用friend关键字
例4:
#include <cstdlib>
#include <iostream>
using namespace std;
class Complex
{
int a;
int b;
public:
Complex(int a, int b)
{
this->a = a;
this->b = b;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
Complex operator+ (const Complex& c2);
friend ostream& operator<< (ostream& out, const Complex& c);
};
ostream& operator<< (ostream& out, const Complex& c)
{
out<<c.a<<" + "<<c.b<<"i";
return out;
}
Complex Complex::operator+ (const Complex& c2)
{
Complex ret(0, 0);
ret.a = this->a + c2.a;
ret.b = this->b + c2.b;
return ret;
}
int main(int argc, char *argv[])
{
Complex c1(1, 2);
Complex c2(3, 4);
Complex c3 = c1 + c2;
cout<<c1<<endl;
cout<<c2<<endl;
cout<<c3<<endl;
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
4.什么时候使用全局函数重载操作符?什么时候使用成员函数重载操作符?
当无法修改左操作数的类时,使用全局函数进行重载
=, [], ()和->操作符只能通过成员函数进行重载
5.不要重载&&和||操作符
&&和||是C++中非常特殊的操作符
&&和||内置实现了短路规则
操作符重载是靠函数重载来完成的
操作数作为函数参数传递
C++的函数参数都会被求值,无法实现短路规则
6.++操作符的重载
++操作符只有一个操作数
++操作符有前缀和有后缀的区分,区分前后缀:C++中通过一个占位参数来区分前置运算和后置运算
例:
obj++:
Complex operator++(int)
{
Complex ret = *this;
a++;
b++;
return ret;
}
++obj:
Complex& operator++()
{
++a;
++b;
return *this;
}