Thread: Clarify pass by reference

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    87

    Clarify pass by reference

    For this simple singly linked list, I thought pass by reference stores address, not the copy of object so it's more efficient, then why do I have to 'dereference' head in main if I want to use insert_at_end function which passes by reference (rather than pass by ptr to ptr). I think I sort of understand, b/c references are the actual objects, then is pass by reference passing address too, but differently than pass by ptr to ptr (pass by address)...

    Just to clear it up.

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    class sll//I should use struct for backwards compatability w/ C (in case I need to work w/ C legacy code or low level programming like bitfields
    //    in a struct to talk to registers/other hardware directly)
    {
        public:
            int data;
            sll* next;
    };
    
    //NB: if you pass an object (class/struct) by reference, the instantiated obj must be passed, NOT a ptr to the obj??
    void insert_at_end(sll& head, int d)
    {
        sll newNode;
        newNode.data = d;
        newNode.next = NULL;
       
        if ( !head )//if empty list 
        {
            head = &newNode;
            return;   
        }
        //else not empty list 
        for ( sll cur = head; cur != NULL; cur = cur.next )
              if ( cur.next == NULL )//if you found tail node, new node linked after it
              {
                    cur.next = newNode;
                    return;
              }
    }
    
    int main()
    {
        sll* head = NULL;
        sll node; node.data = 9000; node.next = NULL;
        head = &node;
        
        insert_at_end(*head, 90);
    
        
        return 0;
    }
    EDIT: actually I just wrote this code out and found out I can't mix reference to objects and w/ straight ptrs to obj (the head var I created in main I mean). But is pass by reference still similar to pass by address where the memory location of the object is passed but references are more controlled syntax...I'll look into it (I found this excellent tutorial for anyone in the future: http://www-cs-students.stanford.edu/...nfo/references)
    Last edited by monkey_c_monkey; 08-29-2012 at 03:37 PM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by monkey_c_monkey View Post
    why do I have to 'dereference' head in main if I want to use insert_at_end function which passes by reference
    You coud have passed node directly

    Code:
    int main() {
        sll node; 
        node.data = 9000; node.next = NULL;
       
        insert_at_end( node, 90);
        return 0;
    }
    Kurt

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Sorry, I didn't look at insert_at_end at all. Just at the question and the call.

    Code:
    void insert_at_end( sll& head, int d)
    {
        sll newNode;
        newNode.data = d;
        newNode.next = NULL;
        
        if ( !head )//if empty list  can never happen references cannot be null
        {
            head = &newNode; // ERROR head is not a pointer
            return;  
        }
        //else not empty list
        for ( sll cur = head; cur != NULL; cur = cur.next ) // ERROR
              if ( cur.next == NULL )//if you found tail node, new node linked after it
              {
                    cur.next = newNode;  // ERROR newNode is not a pointer
                    return;
              }
    }
    Guess you want to pass a reference to a pointer to insert_at_end()
    Kurt

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Your function signature on line 15 is wrong, that's why you're confusing yourself into thinking that you have to pass in *head in main. The answer is that you don't do that, and that will not work for what you want to do.

    You need to fix the function prototype to take a reference to a pointer and in main just pass in the pointer.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-14-2012, 07:45 PM
  2. Pass by reference
    By jrice528 in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2007, 01:02 PM
  3. Pass by reference vs pass by pointer
    By Non@pp in forum C++ Programming
    Replies: 10
    Last Post: 02-21-2006, 01:06 PM
  4. pass by reference .... HELP!!
    By NamelessNoob in forum C++ Programming
    Replies: 19
    Last Post: 02-15-2006, 12:50 PM
  5. pass be reference versus pass by value
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 01:03 PM