1

My question is really straightforward and I believe understandable. I've made this simple snippet to illustrate my conflict when I'm passing values by reference.

int main() {
 int a = 1;
 int &b = a;
}

I know that this is the correct way to do it but how does it make sense to take the address of b and make it equal to the value of a. Logically it should be: int &b = &a;

Ari
  • 324
  • 3
  • 13
  • You cannot. Once a reference refers to something, it can never be changed to refer to something else. – François Andrieux Mar 04 '19 at 15:15
  • 1
    @FrançoisAndrieux I've taken on me to reopen this question. Your comment made me think you might have overlooked the question. Notice _"how does it make sense to take the address of b and make it equal to the value of a"_. – YSC Mar 04 '19 at 15:19
  • `&` does not always mean "take address of". In your code snippet it states the type of `b` is "reference to `int`". – Krzysiek Karbowiak Mar 04 '19 at 15:19
  • You seem to be confusing [reference initialization](https://en.cppreference.com/w/cpp/language/reference_initialization) with the address-of operator. References are initialized using an _object_ of type `T` (`int` in your example) whereas the address-of operator yields a memory address (i.e. a pointer type). – Richard Mar 04 '19 at 15:21
  • Following that logic, you should also write `int *b = *a;`, but you don't; you write `int *b = &a;`. – molbdnilo Mar 04 '19 at 15:23
  • I agree with you that the syntax for references in C++ makes no sense. It's just badly designed (in particular, it is not analogous to `*` / pointers). – melpomene Mar 04 '19 at 15:24
  • @Richard What you're saying is that they just use the same notation (&) but in fact they are doing completely different things under the hood, right? Same as : int* p; p = &a; where the value of a would be equal to *p. They both use * but in a different way. – Ari Mar 04 '19 at 15:31
  • 1
    @Ari It's better to think of them as completely different things that happen to have the same spelling. (That is, homonyms.) – molbdnilo Mar 04 '19 at 15:37

2 Answers2

6

Many operators are context-sensitive. The & "operator" could mean three different things depending on context:

  1. It could be used when defining a reference

    int& r = a;  // The variable r is a reference of the variable a
    
  2. It could be used to get a pointer to something

    int* p = &a;  // Here & is the address-of operator which returns a pointer
                  // Here it makes p point to the variable a
    
  3. It could be the bitwise AND operator

    0x53 & 0x0f    // Here's a bitwise AND operation, the result is 0x03
    
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
4

You're mistaken by the syntax. This is unhappy, but & is both the addressof operator and the token meaning "referente to".

int &b = a;

This declares the variable b to be of type int& (reference to an int) and initialized to a. No address whatsoever.

YSC
  • 38,212
  • 9
  • 96
  • 149