Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reassigning to const reference

With pointers I can do this:

int a=1;
int b=2;

const int* cnstIntPtr = &a;
// (*cnstIntPtr)++ does not work, makes sense since value pointed to is constant
cnstIntPtr = &b; // works, makes sense

const int* const cnstIntCnstPtr  = &a;
// (*cnstIntCnstPtr)++; does not work, makes sense since value pointed to is constant
// cnstIntCnstPtr = &b;  does not work, makes sense since pointer is constant

but with references:

const int& cnstIntRef = a;
// cnstIntRef++; does not work, makes sense since const refers to value at address
// cnstIntRef = b; why does this not work? "expression must be modifiable lvalue"

const int& const cnstIntCnstRef = a;
//cnstIntCnstRef++; does not work, makes sense since const refers to value at address
//cnstIntCnstRef = b; does not work, makes sense since const refers to value at address

So why can't I reassign to a const reference when the const is supposed to be referring to the value at the address (by analogy with how pointers work). If this is in general not possible, why is that and what is the meaning of the second const in

const int& const cnstIntCnstRef?

like image 955
braaterAfrikaaner Avatar asked Dec 12 '25 19:12

braaterAfrikaaner


1 Answers

// cnstIntRef = b; why does this not work? "expression must be modifiable lvalue"

For the same reason as why cnstIntRef++; doesn't work. cnstIntRef is reference to const, and therefore the the value may not be assigned to.

If this is in general not possible, why is that

It is indeed not possible.

References are different from pointers: They are dereferenced automatically. An assignment to a reference variable is an assignment to the referred object. Just as you understand that cnstIntRef++ is analogous to (*cnstIntPtr)++ , you must also understand that cnstIntRef = a is analogous to *cnstIntPtr = a.

As a consequence, there is no syntax to "reassign" a reference to refer to another object. A reference always refers to exactly one object throughout its entire lifetime.


what is the meaning of the second const in

const int& const cnstIntCnstRef?

It has no meaning because it is ill-formed. Unlike to pointers, qualifiers may not be applied to references; they may only be applied to the referred type.


then how do I deal with a std::vector<const int&>

You cannot deal with std::vector<const int&> because const int& is not a valid type for an element of std::vector. Vector requires the elements to be erasable. References are not erasable.

What I need to do is set the size of it, and later in the constructor body fill in the elements.

You can use a vector of pointers instead. Or vector of std::reference_wrapper if that's more convenient for template purposes.

push_back is out of the question since it messes up the references

push_back won't mess up references if you reserve first.

like image 77
eerorika Avatar answered Dec 15 '25 08:12

eerorika



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!