用途:主要用來實現自義定資料型別的遞增
實現遞增過載前,為了可以cout直接輸出自定義資料型別先實現一下左移運算子(<<)的遞增。
2.在寫後置遞增的時候發現在左移過載那裡會報錯,於是將左移過載函式的第二個引數加了const。#include #include using namespace std;
class myintger
;myintger::myintger(/* args */)
myintger::~myintger()
ostream& operator<<(ostream &cout, myintger& myint)//在類內過載左移不能實現需要的結果,所以用全域性函式過載左移。
};myintger::myintger(/* args */)
myintger::~myintger()
ostream& operator<<(ostream &cout, myintger& myint)
myintger operator++(int)//後置++過載 };
myintger::myintger(/* args */)
myintger::~myintger()
ostream& operator<<(ostream &cout, const myintger& myint)
ostream& operator<<(ostream &cout, const myintger& myint)
{ cout《原因:
因為在cout<
C 遞增運算子過載
作用 通過過載遞增運算子,實現自己定義的資料型別 如下 include using namespace std 過載遞增運算子 自定義型別 class myinteger 過載前置 運算子 返回引用是為了一直對乙個資料進行遞增操作 myinteger operator 過載後置 運算子 這個int代...
c 之遞增運算子過載
includeusing namespace std class myinteger 過載 i運算子 返回引用是為了對同乙個資料進行遞增操作 myinteger operator 過載i 運算子,int是佔位符,後置遞增要返回值 myinteger operator int private intn...
12 5 3 遞增運算子過載
示例 實現person類的年齡自增1。建立類並且過載左移運算子 class person string m name int m age ostream operator ostream cout1,const person p 1 前置遞增 如果遞增運算子過載完成,寫出來應該是這樣的 person...