-1

Why does the initialisation of C++ references works without the "adress-of" operator (which is &)?
Isn't the assignment of Object with a type A to a reference with a type A& wrong?
Shouldn't the assignment use the "&" operator on the right side?

int var = 2
int& ref = var  //why can we assign var of type 'int' to ref of type 'int&'?
int& ref = &var //shouldn't it be like that, so that we will assign the ADRESS to the reference?

EDIT: To summarize it:

  • References are alternative names for Objects.
  • The type of References is created by appending '&' to the type of the referenced object.
  • References are the same as the references object in every context.
  • They do not store the memory of the object as pointers do, the presence of the 'adress-of' operator '&' was misleading here.
Skip
  • 6,240
  • 11
  • 67
  • 117
  • 10
    Because it's not a pointer. – Cat Plus Plus Mar 28 '12 at 00:07
  • 4
    "*why can we assign var of type `int` to ref of type `int&`?*" If this is a real question then I think you really need to start over with [a good book](http://stackoverflow.com/q/388242/636019). – ildjarn Mar 28 '12 at 00:11
  • Yeah, I am reading a book and currently I am trying to understand the reasons of why the referencies and pointers exist in C++ but not in C. Surprisingly the Internet is full with explanations about pointers and memory relation, but not about referencies. – Skip Mar 28 '12 at 00:25
  • C predates C++ by quite a bit of time. It was put on top of C to simplify some of the things programmers do a lot in C (though, that's not the only reason, C is really a good language). Dealing with pointers is one of those things that was simplified. It was simplified by adding references. I don't know of anything that can be done with a reference that can't be done with a pointer, but there are things that can be done with a pointer that can't be done with a reference. (Example: default object construction replaces, but is not the same as returning a null if something fails). – David D Mar 28 '12 at 00:43

3 Answers3

3

A reference does not hold the address of the object, as you imply by your comments. That is a pointer. references != pointers.

A reference is an alias to an object which is why that type of assignment works.

Marlon
  • 19,924
  • 12
  • 70
  • 101
  • I know, that compilers implement referencies as pointers. I thought that referencies can be seen as static pointers? – Skip Mar 28 '12 at 00:29
  • You can see them whichever way you like, but what's important is how they are used. I would focus on the differences as opposed to the similarities since the similarities were what confused you. – ShiggityShiggityShwa Mar 28 '12 at 00:42
  • To summarize it: References are alternative names for Objects. The type of References is created by appending '&' to the type of the referenced object. References are the same as the references object in every context. They do not store the memory of the object as pointers do, the presence of the 'adress-of' operator '&' was misleading here – Skip Mar 28 '12 at 01:07
0

int& is a type

&var is an operation to take address.

Same symbol. Totally different menings. Probably designed specifically to keep C++ inaccessible to newcomers.

learnvst
  • 15,455
  • 16
  • 74
  • 121
0

"I know, that compilers implement referencies as pointers"

No, they don't. This is a common source of confusion. When you do this:

int p;
int *r = &p;

You are creating, say, a 32-bit pointer into memory that you can use to manipulate data.

However, when you do this:

int p;
int& r = p;

You aren't using a pointer to p. There is an object p, that may reside in a register, or the stack or the heap, it's not important. The declaration means that r is also referencing that same object. It is not a pointer to the object: if make a pointer, you're making a new object. You are saying "r points to p." However, using a reference, you are saying "r is p." No new object is created (a pointer). For all intents and purposes, int& r is p, because any use of it references (not points to) p.

Let me put it this way:

It's like saying there's this person, Dave. He lives at (made up address)

This points to Dave.

And then he has a nickname, OrgnlDave.

OrgnlDave doesn't point to Dave, it is Dave, it is just another way of saying it. There is only one Dave. Saying "hi OrgnlDave" is equal to saying "hi Dave," which is different from sending Dave a letter.

In pseudocode,

person Dave;
address(Dave, made-up-address) pointer;
person& OrgnlDave means Dave; // and we indicate that by using the &

Hi(pointer);   // Sends Dave a cordial letter
Hi(Dave);      // Says hi to Dave
Hi(OrgnlDave); // Says hi to Dave
std''OrgnlDave
  • 3,912
  • 1
  • 25
  • 34