Reputation: 35
I am a beginner at c++ can anyone explain me this code:
#include <iostream>
void display(int b)
{
std::cout << b << std::endl;
}
int main()
{
int a;
display(a=10);//display 10
std::cout << a << std::endl;//also display 10
return 0;
}
I know we can use = operator to set default values for a function parameters, but here it's in the function call, apparently "disply(a=10)" pass the value 10 to the function and store it in the variable "a" at the sametime.
is this correct coding in c++ and can anyone explain the assignment part?
Upvotes: 2
Views: 1867
Reputation: 145269
The built-in assignment operator =
is right-associative
which means it groups to right, e.g. a = b = c
means a = (b = c)
,
is an lvalue expression that refers to the left hand side.
Note that in C an assignment produces a pure value, while in C++ it produces a reference (in the common language meaning of referring).
This means that you can assign a value to multiple variables:
a = b = c = 12345;
which is parsed as
a = (b = (c = 12345));
which first copies 12345
to c
, then copies c
to b
, then copies b
to a
.
And it means that you can assign to the result of an assignment:
((a = b) = c) = 12345;
which first copies the b
value to a
, then copies the c
value to a
, then copies 12345
to a
, leaving b
and c
unchanged…
In your case, the code
display(a=10);
is equivalent to
a = 10; display( a );
Since the display
function takes an int
by value, this is equivalent to
display( 10 )
but if display
had a reference argument then it could not be rewritten this way.
A common pitfall is to write
if( x = 12345 )
when one means to do a comparison,
if( x == 12345 )
Many compilers will warn about the first if the warning level is upped, as it should be.
More guaranteed ways to detect it include
Using const
everywhere it can be used.
x
can’t be assigned to when it’s const
. This is my preferred solution.
Writing if( 12345 == x )
.
Some people prefer this, but I find it hard to read, and as opposed to const
it only helps to avoid the mis-typing when the writer is already, at that very point, very aware of the problem.
Defining a custom if
construct via a macro.
Technically this works, also for other constructs that use boolean conditions, but in order to be useful such a macro should be short, and this runs the risk of name collision. It's also hard on maintainers who are unfamiliar with the (effectively) custom language.
In C++03 the standard library required that any container element type should be assignable, and the assignable criterion required that a custom assignment operator T::operator=
should return T&
(C++03 §23.1/4) – which is also a requirement on the built-in assignment operator.
Until I learned that I used to define assignment operators with result type void
, since I saw no point in supporting coding of expressions with side-effects (which is generally a bad practice) at the cost of both efficiency and verbosity.
Unfortunately this is a case where in C++ you pay for what you don’t use and generally should not use.
Upvotes: 1
Reputation: 10417
You need to know about =
operator more. Not only is it assign rhs (right hand side) value to lhs (left hand side), but also it refers to the lhs.
Suppose this code:
a = b = c;
is exactly equal to
a = (b = c);
because =
is right-associative.
If c
is 10, the code assign 10 into b, and assign the value of b
into a. So now a == b == c == 10
.
Upvotes: 2
Reputation: 4241
The line
display(a=10);//display 10
equals to:
a = 10;
display(a);
This is because the value of the clause a = 10;
is a
.
I think this answers your question.
Upvotes: 3
Reputation: 1149
It is correct.
display(a=10); //It assigns 10 to a and 10 is passed as the parameter to function.
Upvotes: 0
Reputation: 5527
The assignment <variable> = <value>
in C, C++ is and expression which means it have a value and this value is, of course, the <value>
you've just assigned.
That's the reason why you can assign a value to multiple variables like this:
a = b = c = 1;
because internally it works something like this
a = value of (b = value of (c = 1));
and since the assignment does indeed have a value, the value of (c = 1)
is
1
, value of (b = (c = 1))
is 1
and therefore we get a = 1
. And as a
If the assignment wouldn't be an expression and didn't have a value, we would
get an error, because value of (c = 1)
would not exist and we would get a
syntax error.
So in your code, display(a=10);
means: *set value a
to 10
and pass the
resulting value (which would be 10) as an argument to the function display
.
Upvotes: 0