Passing parameters by reference

This is a discussion on Passing parameters by reference within the C++ Programming forums, part of the General Programming Boards category; Suppose I have a function like this: PHP Code: void Function1 (const  VECTOR3 &  v );  What difference is it when ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    132

    Passing parameters by reference

    Suppose I have a function like this:
    PHP Code:
    void Function1(const VECTOR3v); 
    What difference is it when making it like:
    PHP Code:
    void Function1(const VECTOR3v);
      or
    void Function1(const VECTOR3 v); 
    The function does not return anything into the v variable. So why cant I just use the last (without the pointer) to pass structures in my function;'s parameters?

    What is best and what is used for which situation?
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    Passing by reference means you can change the original object without using any funny dereferencing notation like you have to do with pointers. Since you're passing a const object, the only real gain you have by passing a reference is if the object is larger in memory than a reference. You can get the same effect with a pointer, but then to access the object you'd need to dereference it, once again, references don't require that :-)
    *Cela*

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Here are some pointers.

    // I prefer this type of parameterizing for ADT
    void Function1(const VECTOR3& v);

    // I prefer this type of parameterizing for dynamic objects
    // and when the function will make changes to the object.
    void Function1(const VECTOR3* v);

    // I prefer this type of parameterizing for primitive data types.
    void Function1(const VECTOR3 v);

    Kuphryn

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    I see. Both posts were helpful, but what is ADT?

    If I use * for every structure passing, would it be safe?
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  5. #5
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>but what is ADT?
    Abstract data type :-)

    >>If I use * for every structure passing, would it be safe?
    Sure, as long as you use the pointers properly it's perfectly safe :-)
    *Cela*

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, 08:18 PM
  2. Using Vectors. MinGW warning
    By Viewer in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2009, 03:15 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  5. Textbox
    By maxorator in forum Windows Programming
    Replies: 20
    Last Post: 09-25-2005, 10:04 AM

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