问题
I came across this line when I was doing some laborations:
int home_address = current_pos_ = hash(key, size_);
And I was wondering what it actually does? (not the hashfunction or anything, just the "int variable = variable = value;" thing)
回答1:
That expression is read as:
int home_address = (current_pos_ = hash(key,size_));
It assigns the result of hash(key,size_)
into current_pos_
and it then assigns the value of current_pos_
into home_address
.
回答2:
The assignment operator evaluates to the final value of its left argument, so this code assigns hash(key, size_)
to current_pos_
, and initialises home_address
with the new value of current_pos_
.
The assignment operator can be overloaded to return any value of any type, so in general, the behaviour of this expression is to call the assignment operator of current_pos_
with the result of hash(key, size_)
(perhaps performing implicit conversions), and then to initialise home_address
with the return value of the assignment operator (again perhaps performing implicit conversions).
回答3:
int x = y = 0;
Is the same as
int x = 0;
y = 0;
来源:https://stackoverflow.com/questions/12955121/double-in-initialization