ex4-1-3.cpp

来自「一些学习c++的例题」· C++ 代码 · 共 55 行

CPP
55
字号
#include <iostream>

using namespace std;

class CCounter
{
private:
   unsigned int m_nCounter;

public:
   CCounter() { m_nCounter = 0; }
   CCounter(CCounter& c) { m_nCounter = c.m_nCounter; }

   ~CCounter() {}

   friend CCounter& operator++(CCounter& c);
   friend const CCounter operator++(CCounter& c, int);

   unsigned operator()()
   {
      return m_nCounter;
   }
};

CCounter& operator++(CCounter& c)
{
   c.m_nCounter++;

   return c;
}

const CCounter operator++(CCounter& c, int)
{
   CCounter before(c);

   c.m_nCounter++;

   return before;
}

void main(void)
{
   CCounter c, s;

   cout << "First of all, c=" << c() << ", s=" << s() << endl;

   s = ++c;
   cout << "After s=++c, c=" << c() << ", s=" << s() << endl;

   s = c++;
   cout << "After s=c++, c=" << c() << ", s=" << s() << endl;

   ++ ++c;
   cout << c();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?