ex4-1-2.cpp

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

CPP
49
字号
#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() {}

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

      return *this;
   }

   const CCounter operator++(int)
   {
      CCounter before(*this);

      m_nCounter++;

      return before;
   }

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

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;
}

⌨️ 快捷键说明

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