C++重载运算符小结与注意点
生活随笔
收集整理的這篇文章主要介紹了
C++重载运算符小结与注意点
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
重載運算符需注意:
1.重載=運算符時容易忘記寫返回值。
2.重載賦值運算符時,記得加const,因為賦值操作必須是固定的右值。
3重載時,寫在類中的只能有一個參數(實際有兩個參數,另外一個是this指針,我們看不見而已),需要兩個參數的時候,要寫在類外,用友元在類內聲明。
4.重載遞增運算符時,要注意哪個要加引用,哪個不用加引用。
//重載前置++運算符 返回引用是為了一直對一個數據進行遞增操作MyInteger &operator++() {//先進行++運算m_Num++;//再將自身做一個返回return *this;}//重載后置++運算符//這個int代表的是占位參數,可以用于區分前置和后置遞增MyInteger operator++(int) { //后置遞增返回的是值,不能是引用//先 返回結果MyInteger temp = *this;//后 遞增m_Num++;//最后將記錄結果做返回return temp;代碼如下:
#include <iostream> using namespace std;class Base { public:Base():num(0){}Base(const Base &a){this->num = a.num;int len = strlen(a.name);this->name = new char[len + 1];for (int i = 0; i < len; i++) this->name[i] = a.name[i];this->name[len] = '\0';}~Base(){if (name != nullptr) delete[] name;}Base(const char *name, int num) :num(num){int len = strlen(name);this->name = new char[len+1];for (int i = 0; i < len; i++) this->name[i] = name[i];this->name[len] = '\0';}Base operator+(Base &a){Base tmp;tmp.num = this->num + a.num;int len1 = strlen(this->name);int len2 = strlen(a.name);int len3 = len1 + len2;tmp.name = new char[len3 + 1];for (int i = 0; i < len1; i++) tmp.name[i] = this->name[i];for (int i = len1; i < len3; i++) tmp.name[i] =a.name[i-len1];tmp.name[len3] = '\0';return tmp;}Base operator+( const int a){Base tmp(*this);tmp.num += a;return tmp;}friend Base operator+(const int, Base&);friend ostream&operator<<(ostream &, const Base&);friend istream &operator>>(istream &, Base&);Base operator()(const char a[], int b){if (name != nullptr) delete[] name;int len = strlen(a);name = new char[len + 1];for (int i = 0; i < len; i++) name[i] = a[i];name[len] = '\0';num = b;return *this;}bool operator==(const Base &a){if (name == a.name && num == a.num) return true;int len1 = strlen(a.name);int len2 = strlen(this->name);if (len1 == len2 && num == a.num){for (int i = 0; i < len1; i++){if (name[i] != a.name[i]) return false;}return true;}return false;}bool operator !=(const Base &a){if (*this == a) return false;else return true;}Base &operator=(const Base &a){if (this == &a) return *this;if (name != nullptr) delete[] name;int len = strlen(a.name);name = new char[len + 1];for (int i = 0; i < len; i++) name[i] = a.name[i];name[len] = '\0';num = a.num;return *this;}Base operator++(int){Base tmp(*this);num++;return tmp;}Base &operator++(){num++;return *this;}void print(){cout << "name = " << name << endl;cout << "num = " << num << endl;}private:int num;char *name; };ostream&operator<<(ostream &os,const Base&p) {os << "name = " << p.name << endl;os << "num = " << p.num << endl;return os; }istream &operator>>(istream &in, Base &p) {char c[10000];in >> c;int len = strlen(c);p.name = new char[len + 1];for (int i = 0; i < len; i++) p.name[i] = c[i];p.name[len] = '\0';in >> p.num;return in; }Base operator+(const int a, Base& b) {Base tmp(b);b.num += a;return tmp; }int main() {Base b("xiaom", 12);cout << b << endl;Base x("ADA", 32);x = x;Base c = b + x;cout << c << endl;cout<<x++<<endl;cout << ++x << endl;c = x;cout << c << endl;Base v = c + 32;int k = 3;Base x2 = c + k;Base x3 = k + c;cout << c << endl;cout << v << endl;v("hhh", 30);cout << v << endl;v = (c + 32);return 0; }總結
以上是生活随笔為你收集整理的C++重载运算符小结与注意点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 寻仙手游攻略
- 下一篇: 一机搞定游戏、学习、办公,这台惠普VIC