Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2008
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Understanding C++ References (&)

    I've started functions and am a bit confused about referencing. Look at this code please:

    Code:
    #include <iostream>
    using namespace std;
    
    void duplicate (int& a, int& b, int& c)
    {
      a*=2;
      b*=2;
      c*=2;
    }
    
    int main ()
    {
      int x=1, y=3, z=7;
      duplicate (x, y, z);
      cout << "x=" << x << ", y=" << y << ", z=" << z;
      return 0;
    }
    Now I understand that the "&" within the duplicate parameters allow integers x, y, and z to refer to a, b, c, multiplying them by two, but I don't understand this:

    Code:
    #include <iostream>
    using namespace std;
    
    void prevnext (int x, int& prev, int& next)
    {
      prev = x-1;
      next = x+1;
    }
    
    int main ()
    {
      int x=100, y, z;
      prevnext (x, y, z);
      cout << "Previous=" << y << ", Next=" << z;
      return 0;
    }
    What is being referred to here? I don't understand how y and z suddenly become the result of prev and next. Please help!

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Which part of the second makes it seem different to you than the first?
    In fact, they're nearly identical.
    I don't know if it will help but we can grow them together a little bit and maybe isolate your confusion.

    Code:
    y *= x;
    Is a shortcut for
    Code:
    y = y * x;
    So we can rewrite the first function like this without changing its functionality at all:
    Code:
    void duplicate (int& a, int& b, int& c)
    {
      a = a * 2;
      b = b * 2;
      c = b * 2;
    }
    If we substitute some similarly poorly named variables in the second problem we get:
    Code:
    void prevnext (int x, int& a, int& b)
    {
      a = x-1;
      b = x+1;
    }
    Which doesn't get us anywhere, really, but maybe it makes the similarity a little more apparent. The only difference is the math operation you're using and the fact that the first parameter is passed by-value in the second function.

    Possibly confusing general explanation:

    Each variable in C++ has two components: a value, and an address.
    To go along with this distinction there are three argument conventions:
    1. by value (eg, typename varname with no modifier, int x)
    These values are copied to their new temporary name when you call the function. You cannot modify the caller's copy of a variable passed by value by any means. The address of the passed variable in the caller's scope and the local variable in the function's scope are different. That means if you put new stuff in the function copy it will never extend outside of the function unless you return that value.
    2. by pointer to address (eg, typename* varname, int* x)
    I take it you haven't learned about this yet, but long story short, a 'pointer' is exactly what it sounds like, directions to find a variable in memory. Since a pointer variable is dealing with the same memory, if you change it, you change the value that was passed because there is no copy.
    3. by reference (eg, typename& varname, int& x)
    A reference can be thought of as *exactly the same thing* as the variable it references. It has the same value and the same address. References behave like pointers in that anything you do to them happens to the same memory location as the caller's version of the value, so changes are replicated across the scope of the function. The only difference between references and pointers is semantics-- pointers involve special syntax to toggle between the value they point at and their intrinsic value (the memory address), which generally makes them a pain in the ***.

    To gloss over some nuances, you can say that a by-value parameter passes "value" (duh), pointer passes "address", and reference passes "address AND value"

    In the likely event that didn't help at all, please post again elaborating why you find the second function confusing but not the first.
    Last edited by ralph l mayo; 07-22-2008 at 04:08 AM.

  • Users who have thanked ralph l mayo for this post:

    Laharl1234 (07-22-2008)

  • #3
    New Coder
    Join Date
    Jul 2008
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Excellent. I understand now. So it was only the variable names that made it confusing for me.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •