++運算符重載問題
簡單探討了++運算符的問題:
?1
#include?
<
iostream
>
?2
using?namespace?std;
?3
?4
class
?temp
?5
{
?6
private
?:
?7
????
int
?x?;
?8
public
?:
?9
?temp()
10
?
{x?
=
?
5
;}
11
??
/**/
/*
??void?operator?++(void)
12
???{
13
???????x++;
14
???}
15
??
16
?void?operator?++(?int)
17
???{
18
???????x++;
19
???}
*/
20
???temp
&
?operator?
++
()
21
???
{
22
??????x
++
;
23
???cout?
<<
?
"
++a
"
?
<<
?endl;
24
???
return
?
*
this
;
25
???}
26
27
???temp
&
?operator?
++
(
int
)
28
???
{
29
??????x
++
;
30
???cout?
<<
?
"
a++
"
?
<<
?endl;
31
???
return
?
*
this
;
32
???}
33
???
34
???
void
?show()
35
???
{
36
?????cout?
<<
?x;
37
???}
38
}
;
39
40
int
?main()
41
{
42
/**/
/*
43
??operator?++(void)??=>?++a;
44
??operator?++(int)???=>?a++;
45
*/
46
temp?a;
47
++
a;??
//
只能++a,而a++就會有警告。
48
a
++
;
49
a.show();
50
??system(
"
pause
"
);
51
return
?
0
;
52
}
53

?2

?3

?4

?5



?6

?7

?8

?9

10



11


12

13

14

15

16

17

18

19

20

21



22

23

24

25

26

27

28



29

30

31

32

33

34

35



36

37

38

39

40

41



42


43

44

45

46

47

48

49

50

51

52

53

operator?++(void)??=>?++a;
operator?++(int)???? =>?a++;
++a;? //call operator++(void)。
a++;? //call operator++(int)
posted on 2006-04-28 11:01 soochow_hhb 以java論成敗 以架構論英雄 閱讀(470) 評論(0) 編輯 收藏 所屬分類: Reading