C++的运算符重载:使对象的运算表现得和编译器内置类型一样
1、应用一:自定义复数类:
#include <iostream>
using namespace std;class CComplex {
public:CComplex(int r = 0, int i = 0): mreal(r), mimage(i){}// 指导编译器怎么做CComplex类对象的加法操作CComplex operator+(const CComplex &com) {return CComplex(this->mreal + com.mreal, this->mimage + com.mimage);}CComplex& operator++(){ // ++在前mreal += 1;mimage += 1;return *this;}CComplex operator++(int) { // ++在后//CComplex comp = *this;//mreal += 1;//mimage += 1;//return comp;return CComplex(mreal++, mimage++);}void operator+=(const CComplex &src) {mreal += src.mreal;mimage += src.mimage;}void show() { cout << "real:" << mreal << " image:" << mimage << endl; }
private:int mreal;int mimage;friend CComplex operator+(const CComplex &lhs, const CComplex &rhs); // 友元函数friend ostream &operator<<(ostream &out, const CComplex &src);friend istream &operator>>(istream &in, CComplex &src);
}; CComplex operator+(const CComplex &lhs, const CComplex &rhs) { // 全局函数如何调用内部成员?定义为友元函数return CComplex(lhs.mreal + rhs.mreal, lhs.mimage + rhs.mimage);
}ostream &operator<<(ostream &out, const CComplex &src) {out << "mreal:" << src.mreal << " mimage:" << src.mimage << endl;return out;
}istream &operator>>(istream &in, CComplex &src) {in >> src.mreal >> src.mimage;return in;
}int main() {CComplex comp1(10, 10);CComplex comp2(20, 20);// comp1.operator+(comp2)加法运算符的重载函数CComplex comp3 = comp1 + comp2;comp3.show();CComplex comp4 = comp1 + 20; // comp1.operator+(20) int->CComplex CComplex(int)comp4.show();// 编译器做对象运算的时候,会调用对象的运算符重载函数(优先调用成员方法); // 如果没有成员方法,就在全局作用域找合适的运算符重载函数// ::operator+(30, comp1) 全局函数CComplex comp5 = 30 + comp1;comp5.show();comp5 = ++comp1;//++ --单目运算符 operator++()前置++ operator++(int)后置++comp1.show();comp5.show();comp5 = comp1++;comp1.show();comp5.show();comp5 += comp1;//comp5.show(); //对象信息的输出// cout ::operator<<(cout, comp1) void << endl;// ostream& operator<<(ostream &out, const CComplex &src)cout << comp1 << endl;cin >> comp1 >> comp2;cout << comp1 << comp2 << endl;return 0;
}
2、应用二:自定义String类
#include <iostream>
#pragma warning(disable:4996)using namespace std;class String {
public:String(const char *p = nullptr) {if (p != nullptr) {_pstr = new char[strlen(p) + 1];strcpy(_pstr, p);}else {_pstr = new char[1];_pstr[0] = '\0';}}~String() {delete[] _pstr;_pstr = nullptr;}String(const String &str) {_pstr = new char[strlen(str._pstr) + 1];strcpy(_pstr, str._pstr);}String &operator=(const String &str) {if (this == &str) return *this;delete[] _pstr;_pstr = new char[strlen(str._pstr) + 1];strcpy(_pstr, str._pstr);return *this;}bool operator>(const String &str) const {return strcmp(_pstr, str._pstr) > 0;}bool operator<(const String &str) const {return strcmp(_pstr, str._pstr) < 0;}bool operator==(const String &str) const {return strcmp(_pstr, str._pstr) == 0;}int length() const { return strlen(_pstr); }const char *c_str() const { return _pstr; }//char ch=str6[6]; str6[6]='7'char &operator[](int index) { return _pstr[index]; }//char ch=str6[6]; 不允许修改! str6[6]='7'const char &operator[](int index) const { return _pstr[index]; }
private:char *_pstr;friend String operator+(const String &lhs, const String &rhs);friend ostream &operator<<(ostream &out, const String &str);
};String operator+(const String &lhs, const String &rhs) {//char *ptmp = new char[strlen(lhs._pstr) + strlen(rhs._pstr) + 1];//strcpy(ptmp, lhs._pstr);//strcat(ptmp, rhs._pstr);//return String(ptmp); // 内存泄漏:ptmp 未释放//char *ptmp = new char[strlen(lhs._pstr) + strlen(rhs._pstr) + 1];//strcpy(ptmp, lhs._pstr);//strcat(ptmp, rhs._pstr);//String tmp(ptmp); // 效率太低//delete[] ptmp; //return tmp;String result;delete[] result._pstr; // 释放默认构造的空字符串内存result._pstr = new char[strlen(lhs._pstr) + strlen(rhs._pstr) + 1];strcpy(result._pstr, lhs._pstr);strcat(result._pstr, rhs._pstr);return result;
}ostream &operator<<(ostream &out, const String &str) {out << str._pstr;return out;
}int main() {String strl;String str2 = "aaa";String str3 = "bbb";String str4 = str2 + str3;String str5 = str2 + "ccc";String str6 = "ddd" + str2;cout << "str6:" << str6 << endl;if (str5 > str6) {cout << str5 << ">" << str6 << endl;}else {cout << str5 << "<" << str6 << endl;}int len = str6.length();for (int i = 0; i < len; ++i) {cout << str6[i] << " ";}cout << endl;char buf[1024] = {0};strcpy(buf, str6.c_str());cout << "buf:" << buf << endl;return 0;
}