Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
const box& operator++()  //postfix
	{
		const box& b = *this;

		this->width++;
		this->height++;
		this->length++;

		return b;
	}


here is my code. but this displays incremented values even if lvalue is a constant.

this can be done without problem like this..

C++
box operator++()  //postfix
	{
		const box b = *this;

		this->width++;
		this->height++;
		this->length++;

		return b;
	}


But the thing is I want to do this with lvalue references. I mean I want to return a "box&" thing without incrementing it's values.

How can I do it ?

Thanks in advance.
Posted
Comments
[no name] 30-May-14 9:51am    
These are prefix overloads. Postfix usually implemented using prefix.
M­­ar­­­­k 30-May-14 10:20am    
Well. I want to make ++ a postfix with operator overloading

1 solution

The second version is correct and shouldn't be changed! The return value must be the value of the instance before incrementation. That value can only be a copy - if it were an lvalue and being modified, the result of the combined modification (with the increment) would be undefined.

If you want to modify as well as increment the object, either do the modification before calling the increment operator (in a separate statement), or use prefix increment. That way the order of modifications is unambiguous.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900