C++ operate연산자 오버라이딩.md!

연산자 오버라이딩

개발 편의를 위해 생성된 인스턴스끼리, 혹은 인스턴스와 다른 변수간 연산자를 통해 연산할 수 있도록 하는것.

Point라는 클래스 안에 int형 변수 x, y가 있고 point1과 point2 간 덧셈연산 혹은 곱셈 등 각종 연산을 하고 싶을때 연산자 오버로딩을 통해 가능하다.

1
2
3
4
Point pos1(3,4);
Point pos2(10,20);

Point pos3 = pos1+pos2;

‘+’ 연산자를 오버로딩하면 위처럼 사용할 수 있다!




연산자 오버라이딩 방법은 2가지가 있다.
클래스 맴버 함수로 정의 하거나 클래스 밖에 정의하고 friend 키워드와 같이 사용하거나.

밖에다 정의한 경우
friend Point operator+(const Point &pos1, const Point &pos2);

안에다 정의한 경우
Point& Point::operator-()

위에는 Point pos3 = pos1-pos2; 처럼 사용하기 위해 정의한 -연산자 오버라이딩

아래는 Point pos3 = pos1+pos2; 처럼 사용하기 위해 정의한 +연산자 오버라이딩

사용법은 같지만 어디에 선언하느냐에 따라 코딩이 달라진다.

맴버 함수로 정의해서 사용하는게 friend때문에 2번 키보드 칠 일이 없기 때문에 알기도 쉽고 간단하다(코드 길이만 봐도 클래스 안에 정의한게 더 짧음).

하지만 꼭 밖에 선언해야 하는 경우가 있는데 iostream객체의 cout과 cin연산자를 오버라이딩할 경우다.

1
2
friend ostream& operator<<(ostream& os, const Point& pos);
friend istream& operator>>(istream& is, Point& pos)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>

using namespace std;

class Point
{
private:
int xpos, ypos;

public:
Point(int x=0, int y=0) : xpos(x), ypos(y)
{}

void ShowPosition() const
{
cout<<"["<<xpos<<","<<ypos<<"]"<<endl;
}
void operator+=(const Point &pos1) //pos1+=pos2; --> pos1=pos1+pos2
{
xpos += pos1.xpos;
ypos += pos1.ypos;
}
void operator-=(const Point &pos1)
{
xpos -= pos1.xpos;
ypos -= pos1.ypos;
}

Point& operator-()
{
xpos=-xpos;
ypos=-ypos;
return *this;
}
Point& operator++()
{
xpos++;
ypos++;
return *this;
} //전위 증가
const Point operator++(int)
{
const Point refobj(xpos, ypos); //복사본 생성
xpos++;
ypos++;
return refobj; //반환받을 한 라인에서만 사용하고 사라질 복사본 반환 const로
} //후위증가
friend Point operator+(const Point &pos1, const Point &pos2);
friend Point operator-(const Point &pos1, const Point &pos2);
friend bool operator==(const Point &pos1, const Point &pos2);
friend bool operator!=(const Point &pos1, const Point &pos2);
friend Point& operator~(Point &pos);
friend Point& operator--(Point& ref);
friend const Point operator--(Point& ref, int);
friend Point operator*(int times, Point& ref);
friend Point operator*(Point& ref, int times);
friend ostream& operator<<(ostream& os, const Point& pos);
friend istream& operator>>(istream& is, Point& pos)
;
};

Point operator+(const Point &pos1, const Point &pos2) //기존의 point 값이 변하지 않도록 const키워드를 붙이는것 이 중요하다.
{
Point pos(pos1.xpos+pos2.xpos, pos1.ypos+pos2.ypos);
return pos;
}
Point operator-(const Point &pos1, const Point &pos2)
{
Point pos(pos1.xpos-pos2.xpos, pos1.ypos-pos2.ypos);
return pos;
}

bool operator==(const Point &pos1, const Point &pos2)
{
if(pos1.xpos == pos2.xpos && pos1.ypos == pos2.ypos)
{
return true;
}
else
{
return false;
}
}
bool operator!=(const Point &pos1, const Point &pos2)
{
if(pos1.xpos == pos2.xpos && pos1.ypos == pos2.ypos)
{
return false;
}
else
{
return true;
}
}

Point& operator--(Point& ref)
{
ref.xpos--;
ref.ypos--;
return ref;
} //전위 증가

const Point operator--(Point& ref, int)
{
const Point refobj(ref); //복사본 생성
ref.xpos--;
ref.ypos--;
return refobj; //반환받을 한 라인에서만 사용하고 사라질 복사본 반환

}

Point& operator~(Point &pos)
{
int temp=pos.xpos;
pos.xpos = pos.ypos;
pos.ypos = temp;
return pos;
}

//교환법칙
Point operator*(int times, Point& ref) //int형*point형
{
Point pos(ref.xpos*times, ref.ypos*times);
return pos;
}
Point operator*(Point& ref, int times) //point형*int형
{
Point pos(ref.xpos*times, ref.ypos*times);
return pos;
}

//전역함수로 cout<<pos 가능하게 만들기 os가 cout클래
ostream& operator<<(ostream& os, const Point& pos)
{
os<<'['<<pos.xpos<<','<<pos.ypos<<']'<<endl;
return os;
}
istream& operator>>(istream& is, Point& pos) //값을 집어넣어야 하기때문에 const를 뺴야함
{
is>>pos.xpos>>pos.ypos;
return is;
}

int main()
{
Point pos1(3,4);
Point pos2(10,20);

Point pos3 = pos1+pos2;

pos1.ShowPosition();
pos2.ShowPosition();
pos3.ShowPosition();
pos3=pos2-pos1;

pos3.ShowPosition();
pos3+=pos1;
pos3.ShowPosition();
pos3-=pos2;
pos3.ShowPosition();

Point pos4(1,1);
if(pos3==pos4)
{
cout<<"같습니다"<<endl;
}

pos4=pos1+pos2;

if(pos3!=pos4)
{
cout<<"같지 안습니다."<<endl;
}
pos4=-pos4;
pos4.ShowPosition();
pos4=-pos4;
pos4.ShowPosition();
pos4=~pos4;
pos4.ShowPosition();
cout<< "---------------------------------"<<endl;
(++(++pos1)).ShowPosition();
cout<< "전위끝"<<endl;
pos1.ShowPosition();

pos1++.ShowPosition(); //후위증가는 Pointer&전달한게 아니기 때문에
pos1.ShowPosition();
Point cpy = pos1*3;
cpy.ShowPosition();

cpy = pos1*3;
cpy.ShowPosition();
cin>>cpy;
cout<<cpy;
cpy=pos1;
cpy.ShowPosition(); //디폴트 대입연산자 생성&호출
}