CppPrimer icon indicating copy to clipboard operation
CppPrimer copied to clipboard

ex2.31

Open hhdidid opened this issue 7 years ago • 3 comments

r1 is a int&, while v2 is a const int. Why it is legel?

r1 = v2; // legal, top-level const in v2 is ignored.

hhdidid avatar Apr 03 '19 12:04 hhdidid

This is assignment rather than binding.

KshZh avatar Aug 21 '19 15:08 KshZh

Same question, the example in the book says int &r = ci (const int ci = 42), is illegal, which is same as "r1 = v2", so I think "r1 = v2" should be illegal.

mental1104 avatar Apr 28 '20 14:04 mental1104

At first, I thought it's illegal. Then I wrote a program to verify it: const int v2 = 0; int &r1 = v2; This is illegal; Then I find that r1 has been bound to v1, so r1 represents v1 which is an int type. The appropriate program is: int v1 = 0; const int v2 = 0; int &r1 = v1; r1 = v2; That's right. It's legal as an assignment.

Fanloe avatar Nov 28 '21 02:11 Fanloe