1

I often see statements like below in C++ books regarding reference:

Reference is just another name of the original object. When it is used, it is replaced by the original object (in most cases).

Here is the question: If I bind a const ref to a non-const object, when this const ref being used and replaced by the original object, does the const-ness goes away?

int i = 42;  
const int & r1 = i;  
int & r2 = r1; // Question: shouldn't r1 here just be replaced by the original object, which is **non-const**?
localhost
  • 375
  • 1
  • 3
  • 15

5 Answers5

2

Simple answer: No

Longer answer: You cannot do that. Once you have a const & it will always stay const (unless you do const casting or some other explicit things). This is by design as otherwise const-correctness wouldn't really mean much and also the reason why you cannot compile your code.

dornhege
  • 1,500
  • 8
  • 8
0

First of all, the provided code is not valid, since you cannot initialize non-constant reference with constant reference.

Regarding your question, it might be easier to consider reference as a special kind of pointer to the object, which is automatically referenced (the sentence you quoted is quite misleading).

kayrick
  • 187
  • 4
  • Strange, I find it easier to think of a reference as an alias to one and only one object. Thinking of a reference as a pointer adds all kinds of confusion, especially when you try to "assign" a reference. – juanchopanza Jul 09 '14 at 15:10
  • @juanchopanza References were only invented to allow operator overloading with convenient syntax, as using pointer parameters would require too many & and * symbols in the code. – Neil Kirk Jul 09 '14 at 15:33
0

Your question is confused, anyway, assigning to a reference does not change the constness of the original object.

And your code won't compile without const_cast.

int& r2 = const_cast<int&>(r1);

You can use const_cast for casting away the constness.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • If you're going to do that you'll want to buy a bus ticket to UB Land because that's where you're likely to end up. – Captain Obvlious Jul 09 '14 at 15:15
  • @CaptainObvlious Yes. But I think it's OK for OP's code, because the original variable `i` is not a const variable. – songyuanyao Jul 09 '14 at 15:18
  • _"the original variable `i` is not a const variable"_ - doesn't matter. As soon as they try to modify it (`r1`) they introduce undefined behavior. Introducing or relying on undefined behavior is _never_ OK. – Captain Obvlious Jul 09 '14 at 15:27
  • 1
    @CaptainObvlious It is only undefined behaviour when the original object was declared const. It's not good practice though. – Neil Kirk Jul 09 '14 at 15:36
0

The reason why int & r2 = r1; fails is because you dropped cv-qualifier const when reference-relate r2 to r1.

See C++ standard working draft, n3797, 8.5.3/5:

If T1 is reference-related to T2, cv1 shall be the same cv-qualification as, or greater cv-qualification than, cv2.

modeller
  • 3,770
  • 3
  • 25
  • 49
-1

The books are lying. C++ references are * const pointers in disguise with some special syntax. A const reference can extend the life of a temporary, which pointers can't. That's it.

References were only invented to allow convenient syntax for operator overloading. I still like references and use them, but I'm under no illusions that they are "magical" and "totally different to pointers".

user229044
  • 232,980
  • 40
  • 330
  • 338
Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
  • It is not only the syntax. The semantics are quite different. – juanchopanza Jul 09 '14 at 15:10
  • _"C++ references are pointers in disguise"_ - only as an implementation detail. As far as the language is concerned references are just _aliases_ and have different semantics. The books are correct. – Captain Obvlious Jul 09 '14 at 15:10
  • @CaptainObvlious What's the difference between "aliases and have different semantics" and "pointers in disguise with some special syntax". The second description would cause less confusion to beginners. – Neil Kirk Jul 09 '14 at 15:25
  • @juanchopanza Apart from a const reference can extend a temporary, what is the difference between pointers and references apart from syntax? – Neil Kirk Jul 09 '14 at 15:26
  • References cannot be default-initialized. References cannot be bound to a different object after creation. – juanchopanza Jul 09 '14 at 15:26
  • @juanchopanza Ok I meant to say references are `* const` pointers in disguise with some special syntax. – Neil Kirk Jul 09 '14 at 15:28
  • You can make a `const` pointer point to `nullptr`, and you cannot use assignment syntax on it. So references are at best similar to const pointers, sometimes. You seem to have confused yourself with the whole "references are really pointers" model. – juanchopanza Jul 09 '14 at 16:13
  • @juanchopanza You can't point to nullptr. You can assign a pointer a value from nullptr. nullptr doesn't support the syntax to create an equivalent reference (as it would be invalid). That's a (good) restriction of nullptr. – Neil Kirk Jul 09 '14 at 16:26
  • A const pointer can point to `nullptr`. A reference needs to refer to a valid object. You can't use assignment *syntax* on a const pointer. You can with a reference. So they are the same, except for all the differences. – juanchopanza Jul 09 '14 at 16:49
  • @juanchopanza Yes they are just the syntax differences. Apart from the syntax, and the temporary binding, they are the same. Syntax doesn't exist to assign nullptr to reference. – Neil Kirk Jul 09 '14 at 16:55
  • @juanchopanza Ok. Apart from temporary binding and no reference to null object, what's the semantic difference between references and const pointers? They are corner cases, OP probably never encountered them yet. My point is describe references in terms of existing language feature rather than pretending they are magical and different. See the confusion of the OP from this approach. – Neil Kirk Jul 09 '14 at 17:06
  • Thanks for the detailed discussion. It certainly enlightened me. –  Jul 09 '14 at 19:21
  • @user3424826 I'm glad someone got something out of it!! LOL – Neil Kirk Jul 09 '14 at 19:38