-
https://www.youtube.com/watch?v=BavSqcfb9Ns&list=PLlJhQXcLQBJqywc5dweQ75GBRubzPxhAk&index=67
1. 연산자 오버로딩
https://codaitsme.tistory.com/14?category=1030889
멤버 메서드 활용하기
https://www.youtube.com/watch?v=QaCpk53KuNg&list=PLlJhQXcLQBJqywc5dweQ75GBRubzPxhAk&index=66 1. 멤버 메서드의 선언, 정의 분리하기 #include using namespace std; class Vector2 { public: Vector2(); Vec..
codaitsme.tistory.com
Vector2 Add(Vector2 rhs) { return Vector2(x + rhs.GetX(), y + rhs.GetY()); }
지난번 학습에서는 두 벡터간의 연산을 Add로 표현하였다.
Vector2 c3 = a + b;
그런데 두 벡터간의 연산을 이런식으로 표현하고 싶다면 어떻게 해야하는가?
그럴때는 operator를 사용하면된다.
#include <iostream> using namespace std; class Vector2 { public: Vector2(); Vector2(float x, float y); float GetX() const; float GetY() const; Vector2 operator+(Vector2 rhs) { return Vector2(x + rhs.GetX(), y + rhs.GetY()); } private: float x; float y; }; int main() { Vector2 a(2, 3); Vector2 b(-1, 4); Vector2 c3 = a + b; cout << "a : (" << a.GetX() << ", " << a.GetY() << ")" << endl; cout << "b : (" << b.GetX() << ", " << b.GetY() << ")" << endl; cout << "c3 : (" << c3.GetX() << ", " << c3.GetY() << ")" << endl; } 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; }
메소드명에 "operator"를 입력하고 "+"를 붙여주면 "operator+"가 되는데 이러면 메소드를 호출할 때 "+"를 통해서 호출할 수 있게된다.
#include <iostream> using namespace std; class Vector2 { public: Vector2(); Vector2(float x, float y); float GetX() const; float GetY() const; Vector2 operator+(const Vector2 rhs) const; float x; float y; }; int main() { Vector2 a(2, 3); Vector2 b(-1, 4); Vector2 c3 = a + b; cout << "a : (" << a.GetX() << ", " << a.GetY() << ")" << endl; cout << "b : (" << b.GetX() << ", " << b.GetY() << ")" << endl; cout << "c3 : (" << c3.GetX() << ", " << c3.GetY() << ")" << endl; } 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.GetX(), y + rhs.GetY()); }
코드의 완성도를 위해서 선언과 정의를 분리해줬다.
2. 연산자 오버로딩을 통해 다양하게 구현해보기
#include <iostream> using namespace std; class Vector2 { public: Vector2(); Vector2(float x, float y); float GetX() const; float GetY() const; 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; float x; float y; }; int main() { Vector2 a(2, 3); Vector2 b(-1, 4); Vector2 c1 = a + b; Vector2 c2 = a - b; Vector2 c3 = a * 1.6; Vector2 c4 = a / 4; float c5 = a * b; cout << "a : (" << a.GetX() << ", " << a.GetY() << ")" << endl; cout << "b : (" << b.GetX() << ", " << b.GetY() << ")" << endl; cout << "c1 : (" << c1.GetX() << ", " << c1.GetY() << ")" << endl; cout << "c2 : (" << c2.GetX() << ", " << c2.GetY() << ")" << endl; cout << "c3 : (" << c3.GetX() << ", " << c3.GetY() << ")" << endl; cout << "c4 : (" << c4.GetX() << ", " << c4.GetY() << ")" << endl; cout << "c5 : "<< c5 << endl; } 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.GetX(), y + rhs.GetY()); } Vector2 Vector2::operator-(const Vector2 rhs) const { return Vector2(x - rhs.GetX(), y - rhs.GetY()); } 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.GetX() + y * rhs.GetY(); }
벡터의 사칙연산과 내적을 표현해보았다.
Vector2 c3 = a * 1.6; Vector2 c3 = 1.6 * a; // 구문오류
그런데 a * 1.6은 되지만 1.6 * a는 안되는 현상이 발생한다. 이걸 형태를 바꾸어서 표현하면 아래와 같이 된다.
Vector2 c3 = a.operator*(1.6); Vector2 c3 = (1.6).operator*(a);
첫 줄은 문제가 없지만 두번째 줄은 문제가 생긴다. 기본자료형은 연산자 오버로딩이 불가능하기 때문이다.
이러한 문제를 해결하기 위해 다음의 코드를 전역함수로 추가해주면된다.
Vector2 operator*(const float a, const Vector2 b) { return Vector2(a * b.GetX(), a * b.GetY()); }
'C++' 카테고리의 다른 글
8부 (클래스) 종합문제 - 2 (0) 2021.12.23 8부 (클래스) 종합문제 - 1 (0) 2021.12.23 멤버 메서드 활용하기 (0) 2021.12.23 상수형 매개변수와 상수형 메서드 (0) 2021.12.23 정적 멤버 (2) (0) 2021.12.23