연산자 오버로딩
2022. 4. 10. 12:00ㆍC++
#include <iostream>
using namespace std;
class Vector2{
public:
Vector2();
Vector2(float x, float y);
float GetX() const;
float GetY() const;
private:
float x;
float y;
};
int main(){
}
Vector2::Vector2() :x(0), y(0){ }
Vector2::Vector2(float x, float y): x(x), y(y) { }
float Vector2::GetX() const {return x;}
float Vector2::GetY() const {return y;}
위와 같은 코드가 있을때 아래의 코드는 오류를 발생한다.
int main(void)
{
Vector2 a(2,3);
Vector2 b(-1,4);
Vector2 c = a+b; // 오류발생. 객체끼리 더할수 없음
reuturn 0;
}
물론 아래와 같이 Sum이라는 함수를 만들 수 있다.
Vector2 Sum(Vector2 a, Vector2 b){
return Vector2(a.GetX()+b.GetX(), a.GetY()+b.GetY());
}
int main(void)
{
Vector2 a(2,3);
Vector2 b(-1,4);
Vector2 c = Sum(a,b);
reuturn 0;
}
위의 함수를 전역 함수가 아닌 클래스 내의 멤버함수로 넣어주겠다.
class Vector2{
public:
Vector2();
Vector2(float x, float y);
float GetX() const;
float GetY() const;
static Vector2 Sum(Vector2 a, Vector2 b){
return Vector2(a.x+b.x, a.y+b.y);
}
private:
float x;
float y;
};
class Vector2{
public:
Vector2();
Vector2(float x, float y);
float GetX() const;
float GetY() const;
static Vector2 Sum(Vector2 a, Vector2 b){ //static 선언
return Vector2(a.x+b.x, a.y+b.y);
}
Vector2 Add(Vector2 rhs){ //dynamic 선언
return Vector2(x+rhs.x, y+rhs.y);
}
private:
float x;
float y;
};
int main(){
Vector2 a(1,2);
Vector2 b(3,4);
Vector2 c = a.Add(b);
}
위의 Add함수를 operator로 변경해준다.
class Vector2{
public:
Vector2();
Vector2(float x, float y);
float GetX() const;
float GetY() const;
static Vector2 Sum(Vector2 a, Vector2 b){
return Vector2(a.x+b.x, a.y+b.y);
}
Vector2 operator+(Vector2 rhs){ //Add -> operator+ 로 변경
return Vector2(x+rhs.x, y+rhs.y);
}
private:
float x;
float y;
};
int main(){
Vector2 a(1,2);
Vector2 b(3,4);
Vector2 c = a.operator+(b);
Vector2 c2 = a+b; //operator+ 를 사용했을 때 이와 같이 사용할 수 있다.
}
operator+ 선언을 분리한다.
#include <iostream>
using namespace std;
class Vector2{
public:
Vector2();
Vector2(float x, float y);
float GetX() const;
float GetY() const;
static Vector2 Sum(Vector2 a, Vector2 b){
return Vector2(a.x+b.x, a.y+b.y);
}
Vector2 operator+(const Vector2 rhs) const;
private:
float x;
float y;
};
int main(){
Vector2 a(1,2);
Vector2 b(3,4);
Vector2 c = a.operator+(b);
Vector2 c2 = a+b; //operator+ 를 사용했을 때 이와 같이 사용할 수 있다.
}
Vector2::Vector2() :x(0), y(0){ }
Vector2::Vector2(float x, float y): x(x), y(y) { }
float Vector2::GetX() const {return x;}
float Vector2::GetY() const {return y;}
Vector2 Vector2::operator+(const Vector2 rhs) const{
return Vector2(x+rhs.x, y+rhs.y);
}
다른 연산자에 대해서도 연산자 오버로딩을 해본다.
#include <iostream>
using namespace std;
class Vector2{
public:
Vector2();
Vector2(float x, float y);
float GetX() const;
float GetY() const;
static Vector2 Sum(Vector2 a, Vector2 b){
return Vector2(a.x+b.x, a.y+b.y);
}
Vector2 operator+(const Vector2 rhs) const;
Vector2 operator-(const Vector2 rhs) const;
Vector2 operator*(const float rhs) const;
Vector2 operator/(const float rhs) const;
float operator*(const Vector2 rhs) const;
private:
float x;
float y;
};
int main(){
Vector2 a(1,2);
Vector2 b(3,4);
Vector2 c2 = a+b;
Vector2 c3 = a-b;
Vector2 c4 = a*4;
Vector2 c5 = a/4;
float c6 = a*b;
}
Vector2::Vector2() :x(0), y(0){ }
Vector2::Vector2(float x, float y): x(x), y(y) { }
float Vector2::GetX() const {return x;}
float Vector2::GetY() const {return y;}
Vector2 Vector2::operator+(const Vector2 rhs) const{
return Vector2(x+rhs.x, y+rhs.y);
}
Vector2 Vector2::operator-(const Vector2 rhs) const{
return Vector2(x-rhs.x, y-rhs.y);
}
Vector2 Vector2::operator*(const float rhs) const{
return Vector2(x*rhs, y*rhs);
}
Vector2 Vector2::operator/(const float rhs) const{
return Vector2(x/rhs, y/rhs);
}
float Vector2::operator*(const Vector2 rhs) const{
return x*rhs.x + y*rhs.y;
}
그런데 아래와 같은 코드는 오류를 발생한다.
Vector2 c7 = 1.6 * a; //(1.6).operator+(a);
아래와 같은 함수를 추가함으로써 이를 해결할 수 있다.
Vector2 operator*(const float a, const Vector2 b){
return Vector2(a*b.GetX(), a*b.GetY());
}