views:

586

answers:

3

Hi

I undersatnd that references are not pointers, but an alias to an object. However, I still don't understand what exactly this means to me as a programmer, i.e. what are references under the hood?

I think the best way to understand this would be to understand why it is I can't store a reference in a map.

I know I need to stop thinking of references as syntactic suger over pointers, just not sure how to :/

+6  A: 

The important difference apart from the syntactic sugar is that references cannot be changed to refer to another object than the one they were initialized with. This is why they cannot be stored in maps or other containers, because containers need to be able to modify the element type they contain.

As an illustration of this:

A anObject, anotherObject;
A *pointerToA=&anObject;
A &referenceToA=anObject;

// We can change pointerToA so that it points to a different object
pointerToA=&anotherObject;

// But it is not possible to change what referenceToA points to.
// The following code might look as if it does this... but in fact,
// it assigns anotherObject to whatever referenceToA is referring to.
referenceToA=anotherObject;
// Has the same effect as
// anObject=anotherObject;
Martin B
+8  A: 

They way I understand it, references are implemented as pointers under the hood. The reason why you can't store them in a map is purely semantic; you have to initialize a reference when it's created and you can't change it afterward anymore. This doesn't mesh with the way a map works.

Hope this helps.

Regards,

Sebastiaan

Sebastiaan Megens
+6  A: 

You should think of a reference as a 'const pointer to a non-const object':

MyObject& ~~ MyObject * const

Furthermore, a reference can only be built as an alias of something which exists (which is not necessary for a pointer, though advisable apart from NULL). This does not guarantee that the object will stay around (and indeed you might have a core when accessing an object through a reference if it is no more), consider this code:

// Falsifying a reference
MyObject& firstProblem = *((MyObject*)0);
firstProblem.do(); // undefined behavior

// Referencing something that exists no more
MyObject* anObject = new MyObject;
MyObject& secondProblem = *anObject;
delete anObject;
secondProblem.do(); // undefined behavior

Now, there are two requirements for a STL container:

  • T must be default constructible (a reference is not)
  • T must be assignable (you cannot reset a reference, though you can assign to its referee)

So, in STL containers, you have to use proxys or pointers.

No, using pointers might prove problematic for memory handling, so you may have to:

DO NOT use auto_ptr, there is a problem with assignment since it modifies the right hand operand.

Hope it helps :)

Matthieu M.
Minor point: This `MyObject` is already undefined behavior (you're not initalizing the reference with a valid object) and thus this alone is, according to the C++ standard, allowed to format your hard disk or do other bad things to you. (Nasal demons, anyone?) But that's just a very minor point and otherwise yours is an excellent answer. +1
sbi
Another minor point: it seems to me that default-constructibility is not a requirement (except some methods use default constructed instance for the default argument). (20.1.4.1) I think you meant *copy*-constructible.
UncleBens
Exact, for STL container in general it is not necessary (conceptually). For a map, not using a Default Constructible value would invalidate the use of the subscription operator (-> operator[]) though... and personally I get an error message in VC++2008 if I try to use a type that is not default constructible: "error C2512: 'Special::Special' : no appropriate default constructor available"
Matthieu M.