1.什么是运算符重载 运算符重载是一种函数重载. 运算符函数的格式:operatorop(argument-list)例如,operator+()重载+运算符.其中的op,必须是有效的C++运算符,如operator@()会报错,因为C++中没有@运算符. 2.重载运算符的使用 如下例所示: class Test { public: Test operator+(Test &test); } 调用运算符函数的方式有两种:Test t1;Test t2;1)普通函数调用Test t3 = t1.o…
C++运算符重载函数基础及其值返回状态 运算符重载是C++的重要组成部分,它可以让程序更加的简单易懂,简单的运算符使用可以使复杂函数的理解更直观. 对于普通对象来说我们很自然的会频繁使用算数运算符让他们参与计算,但是对于自定义类的对象来说,我们是无论如何也不能阻止写出像下面的代码一样的程序来的. 例子如下: class Test { //过程省略 } int main() { Test a,c; c=a+a; } 当然这样的代码是不能够通过编译…
=====>友元运算符#include <iostream> using namespace std; class Test { public: Test(int a = 0) { Test::a = a; } friend Test operator +(Test&,Test&); friend Test& operator ++(Test&,int); public: int a; }; Test operator +(Test& temp1,…