double '=' in initialization

我的梦境 提交于 2019-12-31 01:51:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!