
为什么要实现运算符重载在 C 中运算符最初是为内置类型如int、double等定义操作方式。当定义一个新的类自定义类型时编译器并不清楚如何对这个新类型的成员变量应用运算符。通过运算符重载程序员可以明确地告诉编译器对于该类的对象运算符如、-、*等应该如何操作其成员变量从而使自定义类型能够像内置类型一样自然地使用这些运算符。1.加法运算符重载作用实现两个自定义类型数据的加法运算在类内实现加法运算符重载123456789101112131415161718192021222324252627282930#include iostreamusingnamespacestd;classPerson {intage;intmoney;public:Person() : money(0), age(0) {}Person(intval,intval2) : age(val), money(val2) {}Person(constPerson other) {this-age other.age;this-money other.money;}// 类内实现重载 本质Person p3p1.operator(p2)Person operator(constPerson other) {Person p;p.age this-age other.age;p.money this-money other.money;returnp;// 不以引用返回是因为在执行完这个函数后p会被销毁以值传递会调用拷贝构造}voidprint() { cout age money endl; }};intmain() {Person a(18, 50);// 括号法Person b {12, 50};// 隐式转换法Person c a b;c.print();return0;}实现加法运算符重载的operator函数来完成的传入的参数为const Person other的原因有以下的两点1.以引用的方式传递是为了防止调用拷贝构造2.加const修饰是为了防止修改实参在函数中声明了一个Person的对象p返回值是以值的形式返回因为值返回会调用拷贝构造如果是以引用的方式传递当这个函数结束时对象p就会被销毁掉。在类外实现加法运算符重载123456789101112131415161718192021222324252627282930#include iostreamusingnamespacestd;classPerson {intage;intmoney;friendPerson operator(constPerson other1,constPerson other2);public:Person() : money(0), age(0) {}Person(intval,intval2) : age(val), money(val2) {}Person(constPerson other) {this-age other.age;this-money other.money;}voidprint() { cout age money endl; }};// 类外实现重载 本质Person p3operator(p1,p2)Person operator(constPerson other1,constPerson other2) {Person p;p.age other1.age other2.age;p.money other1.money other2.money;returnp;}intmain() {Person a(18, 50);// 括号法Person b {12, 50};// 隐式转换法Person c a b;c.print();return0;}在类外实现加法运算符重载相当于类外函数访问类内的成员变量所以要将这个函数在类中声明为友元函数friend。其他的跟在类内实现加法运算符重载是一样的。本质在类内实现加法运算符重载的本质是Person p3p1.operator(p2在类外实现加法运算符重载的本质是Person p3operator(p1,p2)因为一个是通过对象去调用这个函数它本身也算是一个参数所以只需要传人一个参数。而在类外则是相当于直接调用这个函数所以传入的参数是两个。无论是再类内还是在类外实现加法运算符重载最后调用的方式都是。2.左移运算符重载作用可以输出自定义数据类型要在类外实现因为在类内的话调用格式为对象.operator();无法达到cout123456789101112131415161718192021222324252627282930#include iostreamusingnamespacestd;classPerson {intage;intmoney;public:friendostream operator(ostream o,constPerson p);Person() : age(0), money(0) {}Person(intval,intval2) : age(val), money(val2) {}Person(constPerson other) {cout 调用拷贝构造;this-age other.age;this-money other.money;}// 一般不在函数内进行左移重载/*void operator(ostream cout,const Person p) {}*/};// 类外实现左移运算符重载ostream operator(ostream o,constPerson p) {o p.age p.money endl;returno;}intmain() {Person a(5, 12), b;cout a b endl;return0;}因为是在类外实现的所以在类内要将其设置为友元函数。参数为两个一个为ostream类型的o另外一个为引用类型的对象。返回值为引用类型的ostream类型的ostream因为如果想要进行连续的输出就必须让前一个的结果作为第二个左移运算符的第一个参数如下图在op.age的返回值为o然后它与后面就变成了o 接着又调用这个运算符这样才能完成连续的输出。3.递增运算符重载递增分为两种一种是前一种是后。前是在使用之前就让这个数加1返回的是加1之后的结果。而后是先返回这个数再进行加一操作。前返回的是引用后返回的是值。12345678910111213141516171819202122232425262728293031323334#include iostreamusingnamespacestd;classPerson {intage;intmoney;public:Person() : age(0), money(0) {}Person(intval,intval2) : age(val), money(val2) {}Person(constPerson other) {this-age other.age;this-money other.money;}// 前置的重载 返回引用Person operator() {// 保证是对一个数进行this-age;this-money;return*this;}// 后置的重载 加一个int参数占位符区分 返回值Person operator(int) {Person temp *this;// 用一个局部变量返回之前的结果this-age;this-money;returntemp;}voidprint() { cout age money; }};intmain() {Person p(5, 12);p;p.print();return0;}对前和后都是operator所以为了区分对于后来说需要加一个参数占位符来进行区分。在返回值这块前返回的是引用因为要的是它进行加一之后的结果所以可以直接返回这个对象。而后则返回的是一个值因为要返回的是它加一之前的结果所以需要一个局部变量返回之前的结果。