Passing by reference not always the best

This is a discussion on Passing by reference not always the best within the C++ Programming forums, part of the General Programming Boards category; I think if I'm passing an int into a function, and provided that I am not making changes to the ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    163

    Passing by reference not always the best

    I think if I'm passing an int into a function, and provided that I am not making changes to the int, passing it by value is better than passing it by reference.

    Because passing by value will copy the int onto the stack, so the program can directly access the int value.

    On the other hand, passing by reference will copy the address of the int onto the stack, and the program will need to go to the location of the int to access its value.

    Am I right to say this? Please correct me if I'm wrong...

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    passing an int by reference if you aren't going to modify it is pretty worthless, or any primative actually. The main importance for passing something by reference where you aren't going to modify them is in using classes and structs where you would be passing more than 4 or 8 bytes of data and then you should pass a const reference.

  3. #3
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    if you don't want the original value of the int changed, then you are right.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    ok, thanks guys, for clarifying my doubts... =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 05:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21