现在的位置: 首页 > 综合 > 正文

重载自增

2014年01月20日 ⁄ 综合 ⁄ 共 547字 ⁄ 字号 评论关闭

 #include"iostream.h"
class Counter
{
private:
 int v;
public:
 Counter(){}
 Counter(int v)
 {
  this->v=v;
 }
 Counter operator ++()
 {
   v++;                         也可写为:             ++v;
   return *this;                                      return v;
 }
 Counter operator ++(int)
 {
  Counter t;
  t.v=v++;
  return t;
 }
 void disp()
 {
  cout<<v<<endl;
 }
};
void main()
{
 Counter c1(3),c2(3),c;
 c=c1++;
 cout<<"c=c1++ 后, c=";c.disp();
    cout<<endl;
 cout<<"c=c1++ 后, c1=";c1.disp();
 cout<<endl;
    c=++c2;
 cout<<"c=++c2 后, c=";c.disp();
    cout<<endl;
 cout<<"c=++c2 后, c2=";c2.disp();
 cout<<endl;

抱歉!评论已关闭.