
[C++] 10-3. 교환법칙 문제의 해결
·
공부/C++
Point operator*(int times, Point& ref){ Ponit pos(ref.xpos*times, ref.ypos*times); return pos;}자료형이 다른 두 피연산자를 대상으로 하는 연산기본적으로 연산에 사용되는 두 피연산자의 자료형은 일치해야한다.그리고 일치하지 않으면, 형 변환의 규칙에 따라서 변환이 진행된 다음에 연산이 이뤄져야 한다.그러나 다음 예제에서 보이듯이 연산자 오버로딩을 이용하면, 이러한 연산 규칙에 예외를 둘 수 있다.#include using namespace std;class Point {private: int xpos, ypos;public: Point(int x = 0, int y = 0) : xpos(x), ypos(y) { } void Sh..