In the code,
const int x = 3;
int y = 0;
y += x;
Is there any need to remove the const from x before doing the addition or is this maybe done implicitly in the addition operator definition?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Firstly, the
+=operator is an assignment operator (compound assignment). Its behavior though is equivalent toy = y + xcombination (exceptyis evaluated only once).Secondly, when used as an operand of addition operator (including the RHS of
+=as in your example)xparticipates in the expression as an rvalue, i.e. it is implicitly subjected to so called lvalue-to-rvalue conversion. This conversion immediately discardsconst, since rvalues of non-class types (intin your case) cannot be cv-qualified.