10. 연산자 오버로딩 1
출처: 윤성우 열혈강의 C++ 예제코드
10.1. 연산자 오버로딩의 이해와 유형
- 멤버함수에서 오버로딩하기
#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;
}
Point operator+(const Point &ref) //operator+라는 이름의 함수
{
Point pos(xpos+ref.xpos, ypos+ref.ypos);
return pos;
}
};
int main(void)
{
Point pos1(3, 4);
Point pos2(10, 20);
Point pos3=pos1.operator+(pos2);
pos1.ShowPosition();
pos2.ShowPosition();
pos3.ShowPosition();
return 0;
}
- 전역함수에서 오버로딩하기
#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;
}
friend Point operator+(const Point &pos1, const Point &pos2);
};
Point operator+(const Point &pos1, const Point &pos2)
{
Point pos(pos1.xpos+pos2.xpos, pos1.ypos+pos2.ypos);
return pos;
}
int main(void)
{
Point pos1(3, 4);
Point pos2(10, 20);
Point pos3=pos1+pos2;
pos1.ShowPosition();
pos2.ShowPosition();
pos3.ShowPosition();
return 0;
}
10.2. 단항 연산자의 오버로딩
- 증가, 감소 연산자의 오버로딩(전위증가)
#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;
}
Point& operator++()
{
xpos+=1;
ypos+=1;
return *this;
}
friend Point& operator--(Point &ref);
};
Point& operator--(Point &ref)
{
ref.xpos-=1;
ref.ypos-=1;
return ref;
}
int main(void)
{
Point pos(1, 2);
++pos;
pos.ShowPosition();
--pos;
pos.ShowPosition();
++(++pos);
pos.ShowPosition();
--(--pos);
pos.ShowPosition();
return 0;
}
- 증가, 감소 연산자의 오버로딩(후위증가)
#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;
}
Point& operator++()
{
xpos+=1;
ypos+=1;
return *this;
}
const Point operator++(int)
{
const Point retobj(xpos, ypos); // const Point retobj(*this);
xpos+=1;
ypos+=1;
return retobj;
}
friend Point& operator--(Point &ref);
friend const Point operator--(Point &ref, int);
};
Point& operator--(Point &ref)
{
ref.xpos-=1;
ref.ypos-=1;
return ref;
}
const Point operator--(Point &ref, int)
{
const Point retobj(ref);
ref.xpos-=1;
ref.ypos-=1;
return retobj;
}
int main(void)
{
Point pos(3, 5);
Point cpy;
cpy=pos--;
cpy.ShowPosition();
pos.ShowPosition();
cpy=pos++;
cpy.ShowPosition();
pos.ShowPosition();
return 0;
}
10.3. 교환법칙 문제의 해결
- 교환법칙 충족을 위한 연산자 오버로딩
#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;
}
Point operator*(int times)
{
Point pos(xpos*times, ypos*times);
return pos;
}
friend Point operator*(int times, Point & ref);
};
Point operator*(int times, Point & ref)
{
return ref*times;
}
int main(void)
{
Point pos(1, 2);
Point cpy;
cpy=3*pos;
cpy.ShowPosition();
cpy=2*pos*3;
cpy.ShowPosition();
return 0;
}
10.4. cout, cin 그리고 endl의 정체
- «, » 연산자의 오버로딩
#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;
}
friend ostream& operator<<(ostream&, const Point&);
};
ostream& operator<<(ostream& os, const Point& pos)
{
os<<'['<<pos.xpos<<", "<<pos.ypos<<']'<<endl;
return os;
}
int main(void)
{
Point pos1(1, 3);
cout<<pos1;
Point pos2(101, 303);
cout<<pos2;
return 0;
}