c++---operator重载++,--

xiaoxiao2021-02-28  113

#include<iostream> #include<stdio.h> using namespace std; class INT { friend ostream& operator << (ostream& os,const INT& i); public: INT(int i):m_i(i){printf("m_i...\n");}; INT& operator++() { printf("pre ++\n"); ++(this->m_i); return *this; } const INT operator++(int) { printf("post ++\n"); INT temp = *this; ++(*this); return temp; } INT& operator--() { --(this->m_i); return *this; } const INT operator--(int) { INT temp = *this; --(*this); return temp; } int & operator*()const { return (int&)m_i; } private: int m_i; }; ostream& operator<<(ostream& os,const INT& i) { os << '[' << i.m_i << ']'<<endl; return os; } int main() { INT I(5); cout<< ++I; //cout<< ++I; //cout<< I--; //cout<< --I; //cout<< *I; cout<<endl; }
转载请注明原文地址: https://www.6miu.com/read-43534.html

最新回复(0)