Thread: Passing by refernce of address

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    Passing by reference
    • Doesn't require the address-of (&) operator when passing a variable
    • Doesn't require the dereference (*) operator when changing the value of a reference
    • Guarantees the argument will be an object (you can't have a NULL reference)
    • Guarantees the reference will always refer to the same object


    Pointers are mainly used for memory management, but here's an example to contrast pointers and references:

    Code:
    void f (int& ref)
    {
       ref++; // Pretty
    }
    
    void f (int* ptr)
    {
       (*ptr)++; // Ugly
    }
    
    int main()
    {
       int var = 3;
    
       f (var); // Pass by reference, implicit
       f (&var); // Pass by pointer, explicit
    
       f (NULL); // Oops! Pass by pointer
    
       return 0;
    }
    EDIT: Posted a little too late
    Last edited by rudyman; 09-02-2008 at 09:15 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does this do (Windows API)?
    By EVOEx in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2008, 10:48 AM
  2. I thought pointers were pointers...
    By keira in forum C Programming
    Replies: 19
    Last Post: 08-15-2007, 11:48 PM
  3. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  4. passing by address vs passing by reference
    By lambs4 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2003, 01:25 AM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM