When learning the post increment overload, I found that the post increment cannot be chain programmed. I haven't thought of a solution for a long time. What should I do

CSDN Q & A 2022-01-15 02:32:47
The phenomenon and background of the problem

Post increment cannot be chain programmed

class integer{ friend ostream& operator<<(ostream &cout, integer p);public: integer& operator++() { m_num++; return *this; } integer operator++(int) // In order to distinguish the former ++ And after ++: front ++ use function ++(void) , after ++ Use functions ++(int), This is fixed syntax  { integer temp = *this; m_num++; return temp; }private: int m_num = 0;};ostream& operator<<(ostream &cout, integer p){ cout << p.m_num; return cout;}void test13(){ integer p1; cout << ((p1++)++)++ << endl; cout << p1 << endl;}
Operation results and error reporting contents

Post increment still cannot be added to 3, Can only be added to 1

My solution ideas and tried methods
What I want to achieve

Realize chain programming , Keep adding


thank
Similar articles

2022-01-15

2022-01-15

2022-01-15

2022-01-15