C++中的运算符重载
程序员可以重定义或重载大部分 C++ 内置的运算符。这样就能使用自定义类型的运算符。重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。
运算符重载规则如下:
①、 C++中的运算符除了少数几个之外,全部可以重载,而且只能重载C++中已有的运算符。
②、 重载之后运算符的优先级和结合性都不会改变,但是运算对象的求值顺序无法应用到重载的运算符上。
③、 运算符重载是针对新类型数据的实际需要,对原有运算符进行适当的改造。一般来说,重载的功能应当与原有功能相类似,不能改变原运算符的操作对象个数,同时至少要有一个操作对象是自定义类型。
不能重载的运算符只有五个,它们是:成员运算符“.”、指针运算符“*”、作用域运算符“::”、“sizeof”、条件运算符“?:”。
下面以一个位置坐标position类作为一个例子介绍一些常见的运算符重载,其中position类包含三维坐标x,y,z。主要有输出操作符重载、算术运算符重载、关系运算符重载、自加运算符重载、下标运算符重载。代码中有详细的注解。
overloadtest.h
#pragma once
#include
#include
class Position
{
public:Position() &#61; default;Position(double x, double y,double z) :x(x), y(y), z(z) {};Position(const Position&pos) &#61; default;~Position() &#61; default;friend std::ostream& operator<< (std::ostream& out, const Position& pos);friend bool operator&#61;&#61; (const Position& lpos, const Position& rpos);Position& operator&#43;&#61; (const Position& rpos);Position& operator&#43;&#43; ();Position operator&#43;&#43; (int);Position& operator&#61; (const Position& pos);double& operator[](std::size_t n);const double& operator[](std::size_t n) const;private:double x;double y;double z;
};
Position operator&#43; (const Position& lpos, const Position& rpos);
bool operator!&#61; (const Position& lpos, const Position& rpos);
overloadtest.cpp
#include "overloadtest.h"
std::ostream& operator<< (std::ostream& out,const Position& pos)
{out << pos.x << " " << pos.y << " " << pos.z << " " << std::endl;return out;
}
Position& Position::operator&#43;&#61; (const Position& rpos)
{x &#43;&#61; rpos.x;y &#43;&#61; rpos.y;z &#43;&#61; rpos.z;return *this;
}
Position operator&#43; (const Position& lpos, const Position& rpos)
{Position sum &#61; lpos;sum &#43;&#61; rpos;return sum;
}
bool operator&#61;&#61; (const Position& lpos, const Position& rpos)
{return lpos.x &#61;&#61; rpos.x&&lpos.y &#61;&#61; rpos.y&&lpos.z &#61;&#61; rpos.z;
}
bool operator!&#61; (const Position& lpos, const Position& rpos)
{return !(lpos &#61;&#61; rpos);
}
Position& Position::operator&#43;&#43; ()
{&#43;&#43;x;&#43;&#43;y;&#43;&#43;z;return *this;
}
Position Position::operator&#43;&#43; (int)
{Position res;res &#61; *this;&#43;&#43;(*this);return res;
}
Position& Position::operator&#61; (const Position& pos)
{this->x &#61; pos.x;this->y &#61; pos.y;this->z &#61; pos.z;return *this;
}
double& Position::operator[](std::size_t n)
{if (n < 0 || n>2){std::cout << "越界" << std::endl;assert(n < 0 || n>2);}if (n &#61;&#61; 0)return x;if (n &#61;&#61; 1)return y;if (n &#61;&#61; 2)return z;
}
const double& Position::operator[](std::size_t n) const
{if (n < 0 || n>2){std::cout << "越界" << std::endl;assert(n < 0 || n>2);}if (n &#61;&#61; 0)return x;if (n &#61;&#61; 1)return y;if (n &#61;&#61; 2)return z;
}
main.cpp
#include
#include "overloadtest.h"
using namespace std;
int main()
{Position pos(10, 5.0, 6.0);Position pos1(10, 5.0, 6.0);Position pos2(1.5, 12.3, -5.8);Position pos4(pos);cout << "pos1: " << pos1 << endl;cout << "pos2: " << pos2 << endl;cout << "pos4: " << pos4 << endl;pos2 &#43;&#61; pos1;cout << "pos2: " << pos2 << endl;Position pos3 &#61; pos1 &#43; pos1;cout << "pos3: " << pos3 << endl;if (pos &#61;&#61; pos1){cout << "pos 等于 pos1" << endl;}if (pos !&#61; pos2){cout << "pos 不等于 pos2" << endl;}cout << "&#43;&#43;pos: " << &#43;&#43;pos << endl;cout << "&#43;&#43;pos: " << pos << endl;cout << "pos&#43;&#43;: " << pos&#43;&#43; << endl;cout << "pos&#43;&#43;: " << pos << endl;Position pos5;pos5 &#61; pos;cout << "pos5: " << pos5 << endl;pos5[0] &#61; 100;pos5[1] &#61; 10;pos5[2] &#61; 1;cout << "pos5: " << pos5 << endl;system("pause");return 0;
}
结果