0

I'm pretty new to C++ and I cannot seem to grasp why we would use a referee to a variable instead of just using the variable itself.

#include <iostream>
int main()
{
    using namespace std;
    int ival = 1024;
    int &refVal = ival;
    cout << ival << endl << refVal << ends;
    return 0;
}

What is the point of the reference if it only ever gives the value of ival? Why not just use ival?

  • How would you write a copy constructor? – Kerrek SB Nov 20 '15 at 13:33
  • In the example you give, there's no benefit. There are plenty of places where you need to refer to objects without copying them though. Any time you need to pass a big object into another function for example. – tenfour Nov 20 '15 at 13:45
  • This is a really good [tutorial on references](http://www.cprogramming.com/tutorial/references.html) – Mohamad Elghawi Nov 20 '15 at 13:46

0 Answers0