일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- react native 타입스크립트
- C++
- Expo
- c++ 정보은닉
- react native accessible
- stencil buffer
- 스탠실 버퍼 튜토리얼
- node.js
- react native ios 기기 연결
- 리액트 네이티브 맥
- GitHub
- 싸이월드
- node
- CSS
- javascript
- react native mac
- 스탠실 버퍼 시작
- 벡터와 리스트의 차이
- react native typescript
- c++ using
- 리액트 네이티브 설치 오류
- react-native
- html
- react native typescript navigation
- unity stencil buffer
- cyworld
- react
- react native typescript navigate
- 스탠실 버퍼 사용
- react native
Archives
- Today
- Total
혀니의 이거저거 뿌시기
[C++]10-3.교환법칙 문제의 해결 본문
728x90
교환법칙: A+B의 결과는 B+A의 결과와 같음
연산자를 중심으로 한 피연산자의 위치는 연산 결과에 아무런 영향을 미치지 X
(Ex) 곱셈연산, 덧셈연산)
이를 위해 오버로딩도 교환법칙이 성립되도록 작성해야 함.
#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;
}
결과)
[3, 6]
[6, 12]
요런식으로 할 수 있듬..
728x90
'LANG > C++' 카테고리의 다른 글
[C++]12-1.C++의 표준과 표준 string 클래스 (0) | 2023.05.23 |
---|---|
[C++]10-4.cout, cin 그리고 endl의 정체 (0) | 2023.05.18 |
[C++]10-2.단항 연산자의 오버로딩 (0) | 2023.05.18 |
[C++]10-1.연산자 오버로딩의 이해와 유형 (0) | 2023.05.18 |
[C++]08-3.가상 소멸자와 참조자의 참조 가능성 (0) | 2023.05.16 |