Article 38 - C++ Object Reference

Object referencing can be a challenging problem to debug, especially when there are multiple aliasings; multiple references pointing to the same object.

Referencing is used through the ampersand token (&). Essentially, it means to return the address of this object. If we have an integer abc with value 456 in the memory address 123, then abc gives 456 but &abc gives 123.

Pay attention to the following usages of references. Given a variable, "int object = 123". With a function and its signature "void f1 ( int * ptr )", we invoke it with the following statement, "f1( &object )". But with this other function and its signature, "void f2( int & ref )", we invoke it with this other statement, "f2( object )". In the case of the pointer function (f1), we need to pass in the memory address because the compiler will generate a statement that creates a temporary pointer (namely ptr) to the variable object. If you are not convinced, you may want to read article 37 about C pointers. In the second function (f2), the compiler will also generate a temporary reference (ref) to be passed in; however, the compiler is smart enough to take the memory address of object and not its value.

Be sure to use the correct way to invoke a function with a reference parameter.

A reference cannot be changed after initialization, and initialization must occur at the declaration stage (unless it is a global variable). Thus, it is not possible to change what the reference points to. If you had a reference "int def = &abc", you cannot say "def = 789" and have it point to the memory address 789; instead it changes the value of the object abc from 456 to 789. Do not try to do this, "&def = 789", it just does not make sense. Firstly, the compiler will spit out an lvalue error (left side is not an assignable object). Second, even if it was possible, think about what it semantically means. It is saying, change the address of variable def to 789. How in the planet do you change a physical (or virtual) memory address? This concludes that references are not objects; hence, you cannot apply some basic operators on it, such as memory assignment.

Comments (15)

Posted by anonymous - Nike Air Jordan New School at Sunday, December 9, 2012 11:52 PM

Just wanna input on few general things, The website design is perfect, the written content is real fantastic : D.
[url=http://suparjordanshoes.webs.com/]Nike Air Jordan New School[/url]
<a href="http://suparjordanshoes.webs.com/" title="Nike Air Jordan New School">Nike Air Jordan New School</a>

Posted by anonymous - tiffany at Sunday, February 3, 2013 2:19 AM

find miumiu,chloe,prada is too many type and you may have no idea about such many type. tiffany http://www.tiffany1837-jp.com/

Posted by anonymous - tiffany at Tuesday, February 5, 2013 1:36 PM

find miumiu,chloe,prada ,Following a few latest visits here, even so, is becoming more and more chic. tiffany http://www.tiffany1837-jp.com/

Posted by anonymous - ralph lauren outlet at Thursday, February 21, 2013 8:49 PM

unearth ralph lauren,Subsequent several newest visits listed here, nevertheless, is starting to become progressively more chic. ralph lauren outlet http://www.ralphlaurenshop-de.com/

Posted by anonymous - buy viagra online at Saturday, March 2, 2013 4:01 PM

A6K8hz Appreciate you sharing, great blog article. Cool.

Post a comment

  • Name:
  • Post:
  • Challenge:

Register or login to post comments easier.


Vantasy World Copyright 2011 - 2017. Vantasy World is a project developed by Vantasy Online. Privacy Policy.