Thread: Having a problem with string, not sure where to look

  1. #31
    Registered User
    Join Date
    Apr 2003
    Quote Originally Posted by CompiledMonkey
    Weird, that just printed out "hello", so it didn't nullify the StringBuilder from above.
    The result highlights the distinction between passing by value and "passing by reference". When you pass a pointer to a function in C++ or you pass a reference to a function in C#, it is really passed by value. That means a copy of the address is made for the function and it is assigned to the function parameter variable. If the function then changes the address contained in the local parameter variable, e.g. assigning it null, that does not affect the address contained in the original variable back in main(). However, the local parameter variable does contain a valid address of an object, so the function can use that address to change the object (since no copy of the object is made for the function, the terms "copy of the object" and "original object" don't apply).

    When you pass by value, the function is only restricted from changing the value contained in the specific variable that is sent to the function. If you try the example again and put 'ref' in front of the function parameter variable, then inside the function if you change the address stored in the parameter variable, you will be changing the address stored in the original variable back in Main().

    Also try the example this way: create two references to the same object in Main(). Use 'ref' for the function parameter name, and send one object reference to the function. Use the parameter variable to change the original object, and also assign null to the parameter variable. Then in main() show that the object was changed using your second reference to display the object, and show that the first reference's address was changed to null.

    The class all of this code was in is called Program. So I had to make a reference since the methods outside of main are not static.
    I see.
    Last edited by 7stud; 11-26-2005 at 07:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM