Bogotobogo
contact@bogotobogo.com
References for Built-in Types - 2012
- C++ Home
- String
- Constructor
- Operator Overloading
- Virtual Functions
- Dynamic Cast Operator
- Type Cast Operators
- Class auto_ptr
- References for Built-in Types
- Pass by Value vs. Pass by Reference
- Memory Allocation
- Friend Functions and Friend Classes
- Functors (Function Objects)
- Static Variables and Static Class Members
- Exceptions
- Stack Unwinding
- Pointers
- Pointers II - void pointers & arrays
- Pointers III - pointer to function & multi-dimensional arrays
- Taste of Assembly
- Small Programs
- Linked List Examples
- Binary Tree Example Code
- Templates
- Standard Template Library (STL) I
- Standard Template Library (STL) II - Maps
- Standard Template Library (STL) III - Iterators
- Standard Template Library (STL) IV - Algorithms
- Object Slicing and Virtual Table
- The this Pointer
- Stack Unwinding
- Upcasting and Downcasting
- Object Returning
- Private Inheritance
- Preprocessor - Macro
- C++_Keywords
- fstream: input & output
- Multi-Threaded Programming - Terminology
- Multi-Threaded Programming II - Native Thread for Win32 (A)
- Multi-Threaded Programming II - Native Thread for Win32 (B)
- Multi-Threaded Programming II - Native Thread for Win32 (C)
- Multi-Threaded Programming II - C++ Thread for Win32
- Multi-Threaded Programming III - C++ Class Thread for Pthreads
- Multithread Debugging
- Socket - Server & Client
- Embedded Systems Programming
- Boost
- make
- Debugging Crash & Memory Leak
- Libraries
- C++ API Testing
- Design Patterns in C++
- Algorithms in C++
- Programming Questions and Solutions
- Blackjack with Qt
A reference variable is actually just a pointer that reduces syntactical clumsiness related with pointers in C (reference variables are internally implemented as a pointer; it's just that programmers can't use it the way they use pointers).
As a side note, a reference must refer to some object at all times, but a pointer can point to NULL. In this way, references can be more efficient when we know that we'll always have an object to point to, because we don't have to check against NULL
A reference is an alias for an object. It allows us to manipulate an object in a similar way of using pointer without pointer syntax.
In practice, references are primarily used as parameters to a function such as an object passed into a function.
But in this chapter we'll only deal with references for built-in types.
A reference must be initialized. Once defined, a reference cannot be made to refer to another object and this is why it must be initialized.
int ival = 128; int &rval = ival; // rval is a reference to ival int &rval2; // Error: a reference must be initialized
By changing the value of reference, we can change the value of the referenced original variable.
rval++; // ival is now 129.
Though a reference is a kind of pointer, it is not correct to initialize it to the address of an object, as we would do a pointer.
int ival = 128; int &rval = &ival; // Error: rval is of type int, not int*
How about assigning a non-addressable value (such as literal constant) to the reference?
int &rval2 = 256;
Then, we'll get the error message:
error C2440: 'initializing' : cannot convert from 'int' to 'int &'
A reference always refers to the variable with which it was initialized. We can't resign a reference to refer to another variable. So, for example, the result of the code below might not be obvious:
int herWeight = 110; int& WeightOfHermione = herWeight; int WeightOfHarry = 170; WeightOfHermione = WeightOfHarry; WeightOfHarry = 200;
The line
WeightOfHermione = WeightOfHarry;does not reassign the reference WeightOfHermione so it refers to WeightOfHarry because a reference can't be reassigned. However, because WeightOfHermione is just another name for herWeight, the code
WeightOfHermione = WeightOfHarry;is equivalent to
herWeight = WeightOfHarry;which assigns 170 to herWeight. The final outcome: herWeight becomes 170 and WeightOfHermione still refers to herWeight and it is 170 not 200.
And How about assigning an object of a different type (assuming there is a conversion from the one type to the other) to the reference?
double dval = 3.14; int &rval3 = dval;
Again, we'll get similar error:
error C2440: 'initializing' : cannot convert from 'double' to 'int &'
This is another important feature of reference. For functions, when it is used to match arguments with parameters. With value parameters, we said that implicit type conversion occurs (the value of the argument is converted, if possible, to the data type of the parameter). In contrast, reference parameters require that the matched items must have exactly the same data type.
But things are different if it's a const reference.
const &rval4 = 1024; dval = 3.14; const &rval5 = dval;
The conversion is done by the compiler using temporary object.
Two primary differences between a reference and pointer are:
- a reference must always refer to an object
- The assignment of one reference with another changes the value of the object referenced but not the reference itself
Following code is an example for pointers:
int i1 = 1, i2 = 2; int *ptr1 = &i1, *ptr2 = &i2; p1 = p2;
Here, p1 and p2 now both address the same object whose value is 2. This can be a significant source of program error.
But for references,#include <iostream> using namespace std; int main() { int i1 = 1000, i2 = 2000; int &r1 = i1; int &r2 = i2; cout << "r1=" << r1 << " r2=" << r2 << endl; cout << "&r1=" << &r1 << " &r2=" << &r2 << endl; r1 = r2; cout << "r1=" << r1 << " r2=" << r2 << endl; cout << "&r1=" << &r1 << " &r2=" << &r2 << endl; }with outputr1=1000 r2=2000 &r1=0017FF28 &r2=0017FF1C r1=2000 r2=2000 &r1=0017FF28 &r2=0017FF1C
It is i1, the value referenced by r1, that is changed, and not the reference. After the assignment, the two references still refer to their original objects. -
Pinter Reference Assignment to a pointer changes the pointer's value but not the pointed-to-value. Assignment to a reference changes what the reference refers to but not the reference itself. Assignment of pointers does not deep copy, it just assigns to the pointer object itself. Assignment of references does deep copy, it assigns to the referred-to object. We cannot make a reference refer to a different object after initialization. To get a pointer we need to use new or & To access an object pointed to by a pointer, we need to use * or [] -
See also, Pointers vs References
For pointer example:
int *ptr = 0;Here, the pointer, ptr, currently addresses no object, but for reference,
const int &r = 0;internally, following takes place and the reference, r, still refers an object.
int temp = 0; const int &r = temp;
For references with a structure and classes, please try other chapters of this tutorial.
- C++ Home
- String
- Constructor
- Operator Overloading
- Virtual Functions
- Dynamic Cast Operator
- Type Cast Operators
- Class auto_ptr
- References for Built-in Types
- Pass by Value vs. Pass by Reference
- Memory Allocation
- Friend Functions and Friend Classes
- Functors (Function Objects)
- Static Variables and Static Class Members
- Exceptions
- Stack Unwinding
- Pointers
- Pointers II - void pointers & arrays
- Pointers III - pointer to function & multi-dimensional arrays
- Taste of Assembly
- Small Programs
- Linked List Examples
- Binary Tree Example Code
- Templates
- Standard Template Library (STL) I
- Standard Template Library (STL) II - Maps
- Standard Template Library (STL) III - Iterators
- Standard Template Library (STL) IV - Algorithms
- Object Slicing and Virtual Table
- The this Pointer
- Stack Unwinding
- Upcasting and Downcasting
- Object Returning
- Private Inheritance
- Preprocessor - Macro
- C++_Keywords
- fstream: input & output
- Multi-Threaded Programming - Terminology
- Multi-Threaded Programming II - Native Thread for Win32 (A)
- Multi-Threaded Programming II - Native Thread for Win32 (B)
- Multi-Threaded Programming II - Native Thread for Win32 (C)
- Multi-Threaded Programming II - C++ Thread for Win32
- Multi-Threaded Programming III - C++ Class Thread for Pthreads
- Multithread Debugging
- Socket - Server & Client
- Embedded Systems Programming
- Boost
- make
- Debugging Crash & Memory Leak
- Libraries
- C++ API Testing
- Design Patterns in C++
- Algorithms in C++
- Programming Questions and Solutions
- Blackjack with Qt