C++的递增运算符重载
用途:主要用来实现自义定数据类型的递增
实现递增重载前,为了可以cout直接输出自定义数据类型先实现一下左移运算符(<<)的递增。
实现左移递增
#include <iostream>
#include <string>
using namespace std;
class MyIntger
{
friend ostream& operator<<(ostream &cout, MyIntger& myint);//考虑到m_nums是私有的,用友元。
private:
int m_num;
public:
MyIntger(/* args */);
~MyIntger();
};
MyIntger::MyIntger(/* args */)
{
int m_num=0;//在构造的时候初始化
}
MyIntger::~MyIntger()
{
}
ostream& operator<<(ostream &cout, MyIntger& myint)//在类内重载左移不能实现需要的结果,所以用全局函数重载左移。
{//因为cout对象只能有一份,所以用引用。
cout<<myint.m_num;
return cout;//返回引用
}
int main() {
MyIntger myint;
cout<<myint;
system("pause");
}
前置递增的重载
#include <iostream>
#include <string>
using namespace std;
class MyIntger
{
friend ostream& operator<