일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 최소제곱법
- 회귀학습
- virtual function
- inheritance
- 비선형 함수
- Linear Regression
- least square
- overriding
- Effective C++
- 선형모델
- regression
- kubernetes
- member function
- Machine learning
- 지도학습
- 비선형모델
- call by value
- 딥러닝
- 머신러닝
- c++
- MapReduce
- 회귀
- struct
- Class
- local minimum
- Overloading
- call by reference
- Nonlinear Function
- Nonlinear Model
- 맵리듀스
- Today
- Total
J's Study log
[C++] Operator Overloading 2 본문
How to distinguish Prefix Increment Operator from Postfix Increment Operator
The code shows that ++ and -- operator gets different meaning when it is before the operand or after it.
int main(void)
{
int num = 0;
cout<<num++<<endl;
cout<<++num<<endl;
return 0;
}
Just put 'int' as a parameter in postfix increment operator overloading function.
Operator | Overloading function | |
++pos | ⇒ | pos.operator++( ); |
pos++ | ⇒ | pos.operator++(int); |
--pos | ⇒ | pos.operator--( ); |
pos-- | ⇒ | pos.operator--(int); |
If there is another parameters declare like the following.
operator++(Point &ref, int);
const Object & const Return Type
const Point operator++(int)
{
const Point retobj(xpos, ypos);
xpos+=1;
ypos+=1;
return retobj;
}
const Point operator--(Point &ref, int)
{
const Point retobj(ref);
ref.xpos-=1;
ref.ypos-=1;
return retobj;
}
As you know const declared function can only call const members.
While returning the temporary object in the overloaded operator, the temporary object is created as const object.
So following the charactoristic of const, this type of code occurs an error.
(pos++)++;
(pos--)--;
After calculation of primary operator is like this.
(Point-type temporary const object)++;
(Point-type temporary const object)--;
is same as..
(Point-type const temporary object).operator++(); //calling 'operator++(int)' function
operator--(Point-type const temporary object); //calling 'operator--(Point &ref, int)' function
The member function 'operator++(int)' is not a const declared function so an error occurs because the const object called it.
The global function 'operator--(Point &ref, int)' have a parameter which is not declared in const, so the error will occur.
Surprisingly, this error is intended error.
Because, C++ don't allow this kind of operation.
(pos++)++;
So the error is the result of reflecting operation charicteristic of C++.
Commutative Property
class Point
{
private:
int xpos, ypos;
public:
Point(int x=0, int y=0) : xpos(x), ypos(y)
{ }
void ShowPos() const
{
cout<<'('<<xpos", "<<ypos<<')'<<endl;
}
const Point operator*(int times)
{
Point pos(xpos*times, ypos*times);
return pos;
}
};
int main(void)
{
Point pos(1, 2);
Point cpy;
cpy=pos*2;
cpy.ShowPos();
cpy=pos*2*3;
cpy.ShowPos();
return 0;
}
Result
(2, 4)
(6, 12)
With this code it's not able to compile the following code.
cpy = 3 * pos;
But operator '*' must follow the Commutative Property.
So you just need to make another operator overloading function for 'operator*()'.
Global Overloading
Point operator*(int times, Point& ref)
{
Point pos(ref.xpos*times, ref.ypos*times);
return pos;
}
Or you can just use the overloading function like this.
Point operator*(int times, Point& ref)
{
return ref*times;
}
[참고자료]
- 윤성우의 열혈 C++ 프로그래밍
'Computer Language > C++' 카테고리의 다른 글
[C++] Operation Overloading - Assignment Operator (0) | 2020.05.30 |
---|---|
[C++] Operation Overloading - How 'std' works (0) | 2020.05.25 |
[C++] Operator Overloading 1 (0) | 2020.05.02 |
[C++] Default Value of the Parameter (0) | 2020.05.02 |
[C++] Multiple Inheritance (0) | 2020.04.30 |