# include <stdio.h>

int abc(int & x)
{
	return x;
}

int main()
{
	int y = 0;
	abc (y++); // error here
	return 0;
}

error is error C2664: 'abc' : cannot convert parameter 1 from 'int' to 'int &'
But if i call the same function as abc ( y ) ; Then it shows no error. Can some one tell me the reason

Recommended Answers

All 7 Replies

start quote:

# include <stdio.h>

abc (y++); // error here

end quote.

This result surprised me too. I guess it is because

y++

passes the variable into the function as y
and then tries to further alter the variable after entering the function which adds potential undefined behaviour so the compiler assumes a mistake.

as you could alter the value of y in the function.

you could have

abc(++y);

because the value of y is set before the function calls it

The comiler can see the different return type for the two operators as can be seen here:
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

but I am sure someone could give you a clearer explanation

Change line 11 from abc (y++); to abc (++y); and it will work.

This way you make sure the increment is done before the evaluated result is sent to your function. The other way around, you try to increase y after it has been used, which in this case makes it invalid as a reference.

Cheers
Emil Olofsson

Edit: Ah, got beaten to it by 2 minutes :)

What are you trying to accomplish? Do you want the function abc() to run based on zero/NULL or one (1)? The issue is the result of when the increment operator gets applied.

Edit: eh ninja'd

i m not trying to accomplish any thing. Just trying to know the reason that why it behaves like this?

it works on abc ( ++y );
but not on abc ( y++ );

Operators are nothing more than functions that are called by a different means. When you use an operator, it is actually called as a member function by the operand located on the left side of it. The operands (variables) used with the operators become parameters for an operator function which then does the appropriate processing of the information to produce the desired result. It is this mechanism that allows us to overload operators to make them useful for user-defined classes.

The pre-increment (++y) and post-increment (y++) versions of the function responsible for operator++ have different prototypes. As a result, they handle the operand and return the answer not only in different ways, but at different times. The way the post-increment version handles the operand and return value is not compatible with most function calls.

The postincrement is defined to return int . The preincrement is defined to return int& . In other words, y++ yields an lvalue, and ++y does not. That is, the result of y++ cannot be referenced. It can be const referenced however.

void foo(int & x);
void bar(const int & x);
void baz(int x)
{
	foo(x++);
	bar(x++);
}

testing.cpp(5) : error C2664: 'foo' : cannot convert parameter 1 from 'int' to 'int &'
testing - 1 error(s), 0 warning(s)

As you can see, no complaints on bar .

PS: All speculations based on the exact moment when the value of y changes are wrong. Remember, the function call is a sequence point. It means that when the call happens, all side effects are already completed - no matter which form of the increment is used.

PPS:
The rationale on this difference between pre and post forms of increment is approximately following:
Results of some expressions, such as a = b , ++i , x += y are bound to a variable. Loosely speaking, they have their respective addresses. Results of some other expressions, such as a + b , i++ , etc are not bound to anything. They just exist. There's no address which may refer to them.

thanks a lot nezachem. I m very thankful to you. FBODY and emilo and tetron all of you thanks a lot. I m very thankful to all of you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.