call by reference
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: call by reference

Hybrid View

  1. #1
    Join Date
    Sep 2003
    Posts
    815

    call by reference

    Hello,

    forgive my ignoricy, but if I understand correctly there are 3 ways for passing function parameters

    1. call by value - which make a temp object -> do not change my value
    2. using pointers
    3. using reference

    2 & 3 do change the value, but what is the difference between them?
    is one of them more efficent than the other?
    is any of them do a copy operation for the entire struct being send as parameter (like in call by value)?

    thank you very much
    avi (123)

  2. #2
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    The main difference is that when calling by pointer, the value can be 0. With reference, it needs to be something valid.

    There's no difference in performance between the two.

    If you don't want to allow the function to change the value, make sure you make the parameter const.

    Jeff

  3. #3
    Join Date
    Sep 2003
    Posts
    815
    I know that while using call by value the entire object (instead of a pointer) is sent/copied to the function

    and I know that is not the situation while using pointers as function parameters, but what happend when you send the object by reference does the entire object is sent?

    I mean when my func looks like that MyFunc(MyObject& obj)
    what is sent the entire object (which is a heavy operation) or a pointer? (like hen using pointers)

    thanks again

  4. #4
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    It's like a pointer. Nothing is copied.

    Jeff

  5. #5
    Join Date
    Feb 2002
    Posts
    4,640
    Pass by reference doesn't copy the object at all. There should be no difference in passing an object by reference, or passing in a pointer to an object.

    If you have a large object, that you are passing into functions, you're probably better off declaring the functions to accept a const reference (as mentioned before).
    Code:
    class SomeBigObjClass;
    ...
    int MyFunc (const SomeBigObjClass &a)
    {
      // I cannot modify a in here, only read from it
    }
    Unless, of course, your function can take no object at all (i.e. a NULL pointer).

    Viggy

  6. #6
    Join Date
    Sep 2003
    Posts
    815
    so what is the difference betwenn using opinters & using reference as functions parameter?

    why do both way exist

    and which way is better when I need to pass my function an object that I would like to change
    I mean from 2 aspects 1. efficency 2. readabilty

    thanks
    avi

  7. #7
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    If you are just passing an object, using a reference is better in terms of readability. As from the perspective of efficiency, both are the same.

    However, if you need to pass a pointer to an array of object, there is no choice but to use a pointer. This is because the address stored by the reference cannot be modified once after initialization.

  8. #8
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    References behave better sytactically when you are using objects with operator overloads.

    Case in point:

    Code:
    void method1(const std::string& str)
    {
         if( str[0] == "%" )
         {
              ...
         }
    }
    
    void method2(const std::string* str)
    {
         if(  (*str)[0] == "%"  )
         {
              ...
         }
    }
    In method2, you need to dereference first before using the overloaded operators. Pointers are not objects and don't behave like them. They are necessary largely because of the C heritage, and they also do have their uses as stated in other responses. I would say that references are more of the correct OO approach.

    Jeff

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center