[cs13001] assignment operator as an argument in pass-by-reference parameter
Mikhail Nesterenko
mikhail at cs.kent.edu
Wed Feb 19 20:12:51 EST 2014
CSI students,
This has come up on the lecture today. It turns out it is officially
legal (according to the latest -- C++11 standard) to use assignment as
well as compound assignment as arguments for pass-by-reference
parameters. That is given
void myFunc(int &); // function prototype
// that takes one pass-by-reference
// parameter
It is legal to invoke it something like
int i;
myFunc(i=5);
myfunc(i+=5);
The reason is the same as to why assignment statements may be stacked:
a = b = c;
The assignment returns the memory location of its left hand side (and
hence can be passed by reference).
Nevertheless, while stacked assignment is clear and well-known, using
assignment as an argument, although syntactically legal, is obscure,
hard to read, and is to be avoided.
Thanks,
--
Mikhail
More information about the cs13001
mailing list