-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6_2_IntWrapper.cpp
More file actions
62 lines (53 loc) · 1.13 KB
/
6_2_IntWrapper.cpp
File metadata and controls
62 lines (53 loc) · 1.13 KB
1
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
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <iostream>
class Int
{
int data;
public:
Int() : data(0) {};
Int(int data) : data(data) {};
Int(const Int& c) : data(c.data) {};
operator int() const;
Int& operator++();
Int operator++(int);
int get_data() const { return data; };
};
Int::operator int() const
{
return data;
}
Int& Int::operator++()
{
printf("전위 증감 연산자\n");
++data;
return *this;
}
Int Int::operator++(int)
{
printf("후위 증감 연산자\n");
Int temp(data);
++data;
std::cout << "temp: " << temp.get_data() << " origin: " << data << std::endl;
return temp;
}
void Print(const Int& a) // Int 와 Int& 모두 받을 수 있음
{
printf("Int: %d\n", a.get_data());
}
int main(void)
{
Int x(3);
// x => [x.operator int()] => (int) 3
int a = x + 4;
printf("%d\n", a);
x = a * 2 + x + 4; // x.operator int()와 default operator=
printf("%d\n", x); // 여기서는 x.operator int() 작동 X
std::cout << x << std::endl; // 여기서는 x.operator int() 작동 O
printf("\n");
Int t(3);
//Print(++t);
std::cout << ++t << std::endl;
//Print(t++);
std::cout << t++ << std::endl;
std::cout << t << std::endl;
}