首页 > 编程语言 > 详细

C++ 运算符重载++

时间:2014-03-27 04:42:14      阅读:632      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
 1 #include <iostream>
 2 using namespace std;
 3 
 4 class Good
 5 {
 6 public:
 7     int member;
 8     Good(int a): member(a)
 9     {
10         cout << "调用构造" << endl;
11         cout << "member =" << member;
12     }
13     void show()
14     {
15         cout << member << endl;
16     }
17     Good operator+(const Good& obj)
18     {
19         return Good(member - obj.member);
20     }
21     
22     friend ostream& operator<<(ostream& os, Good& obj)
23     {
24         os << obj.member << "运算符重载" << endl;
25         
26         return os; //这句话关键,便于重复调用 
27     }
28     
29     Good& operator++()
30     {
31         member++;
32         return *this;
33     }
34     
35     Good operator++(int)
36     {
37         //下面的注释部分是本函数实现的另一种写法 
38         Good old(*this);
39         member++;
40         return old;
41     }
42     
43 //    friend Good operator++(Good& obj, int)
44 //    {
45 //        Good old(obj);
46 //        obj.member++;
47 //        
48 //        return old;
49 //    }
50 };
51 
52 int main(int argc, char *argv[])
53 {
54     Good a(10), b(20);
55     Good c = a + b;
56     cout << c;
57     ++c;
58     cout << c;
59     c++;
60     cout << c;
61     c.show();
62     return 0;
63 }
bubuko.com,布布扣

C++ 运算符重载++,布布扣,bubuko.com

C++ 运算符重载++

原文:http://www.cnblogs.com/autumoonchina/p/3627228.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!