J's Study log

[C++] Operator Overloading 2 본문

Computer Language/C++

[C++] Operator Overloading 2

정우섭 2020. 5. 7. 22:31

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++ 프로그래밍

Comments