Having a problem with string, not sure where to look

This is a discussion on Having a problem with string, not sure where to look within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by 7stud The 'new' operator returns an address of some memory, and an address has to be assigned ...

  1. #16
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by 7stud
    The 'new' operator returns an address of some memory, and an address has to be assigned to a pointer.
    Oh, thats exactly how it works, thanks for the correction.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  2. #17
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    7stud, so in C# you can achieve the same result using "ref" in your function parameters. This will pass a variable by reference, otherwise it makes a copy of the passed object (pass by value).

  3. #18
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    7stud, so in C# you can achieve the same result using "ref" in your function parameters. This will pass a variable by reference, otherwise it makes a copy of the passed object (pass by value).
    I'm not familiar with the syntax of C#. From what I hear, it's supposed to be a Java like language, so it might work the same way as Java: you just pass the object name(which is a pointer so only an address gets copied).

  4. #19
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Quote Originally Posted by 7stud
    I'm not familiar with the syntax of C#. From what I hear, it's supposed to be a Java like language, so it might work the same way as Java: you just pass the object name(which is a pointer so only an address gets copied).
    Works the opposite it seems. If you don't specify the "ref" before the variable in the method def, it will copy the object into the function. If you use the ref keyword, you will be manipulating the object sent to the function.

  5. #20
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by CompiledMonkey
    Works the opposite it seems. If you don't specify the "ref" before the variable in the method def, it will copy the object into the function. If you use the ref keyword, you will be manipulating the object sent to the function.
    Based on this description of 'out' and 'ref':

    http://www.c-sharpcorner.com/Language/out_and_ref.asp

    I think you are misunderstanding some basics. Primitive types like int and double are not "objects" so their names are not pointers. When you pass a primitive type in Java or C#, you are "passing by value". If you understand "passing by value" and its implications, i.e. you can't change the original variable, then 'out' and 'ref' allow you to do some things you can't do when passing by value in C++. However, in C++ you don't have to pass primitives by value. In C++, you can achieve the same thing as 'ref' by passing a pointer to the primitive type.
    Last edited by 7stud; 11-25-2005 at 09:19 PM.

  6. #21
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Yes, I understand that int/double/etc are primitive types, but I assumed we're talking about user defined data types, such as my Book object above. Which, when used with ref:

    Code:
    public void Blah(ref Book b) {}
    will manipulate the actual Book object you passed in, rather than copy the object and only have its scope visible in that function.

  7. #22
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by CompiledMonkey
    Yes, I understand that int/double/etc are primitive types, but I assumed we're talking about user defined data types, such as my Book object above. Which, when used with ref:

    Code:
    public void Blah(ref Book b) {}
    will manipulate the actual Book object you passed in, rather than copy the object and only have its scope visible in that function.
    Sorry, that reference I cited used 'ref' in conjunction with primitive types, so I thought they didn't apply to objects. Are you saying you can't alter 'b' without using 'ref'?

  8. #23
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by CompiledMonkey
    7stud, so in C# you can achieve the same result using "ref" in your function parameters. This will pass a ...
    Yes.

    Code:
    If you don't specify the "ref" before the variable in the method def, it will copy the...
    Yes.

    and yes for the rest.

    That is exactly the same as its done in C++.

    ref Book b == Book* b.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  9. #24
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Quote Originally Posted by 7stud
    Are you saying you can't alter 'b' without using 'ref'?
    Correct. If you don't specify ref, it will always get passed by value. You can always return the Book type, so:

    Code:
    public Book Blah(Book b)
    {
      // do some stuff to b
      return b;
    }
    
    obj.b = Class.Blah(obj.b);
    But that seems pointless, so just use ref in that case.

  10. #25
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by CompiledMonkey
    Correct. If you don't specify ref, it will always get passed by value. You can always return the Book type, so:

    Code:
    public Book Blah(Book b)
    {
      // do some stuff to b
      return b;
    }
    
    obj.b = Class.Blah(obj.b);
    But that seems pointless, so just use ref in that case.
    Thats right. Thats how its done in C++, and what was the question again?
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  11. #26
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Quote Originally Posted by Dae
    Thats right. Thats how its done in C++, and what was the question again?
    Haha, I think we've just been enjoying a learning session here. I've learned a lot.

  12. #27
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by 7stud
    I'm not familiar with the syntax of C#. From what I hear, it's supposed to be a Java like language, so it might work the same way as Java: you just pass the object name(which is a pointer so only an address gets copied).
    Quote Originally Posted by CompiledMonkey
    Works the opposite it seems. If you don't specify the "ref" before the variable in the method def, it will copy the object into the function. If you use the ref keyword, you will be manipulating the object sent to the function.
    I read up on C# types and parameter passing, and from what I can tell if you pass an object of a user defined class to a function, then you are passing a pointer, so the function can use that pointer to change the original object.
    Simple types (such as float, int, char), enum types and struct types are all value types.

    Class types, interface types, delegate types and array types are all reference types.
    ----
    Quote Originally Posted by CompiledMonkey
    Correct. If you don't specify ref, it will always get passed by value.
    Yes, the tutorial mentions this:
    There are four different kinds of parameters in C#: value parameters (the default), reference parameters (which use the ref modifier)...
    However, when you pass a pointer/reference by value, the function gets a copy of the address--but a copy of an address can still access the original object (pointers also get passed by value in C++; it's just called "passing by reference"). Here is an example from the tutorial I read:
    Code:
    void Foo (StringBuilder x)
    {
        x.Append (" world");
    }
    
    ...
    
    StringBuilder y = new StringBuilder();
    y.Append ("hello");
    Foo (y);
    Console.WriteLine (y);
    
    output: hello world
    (I have used StringBuilder as a random example of a reference type - there's nothing special about it.)
    Can you get that example to work?

    According to the tutorial, since your Book object is a class:
    but I assumed we're talking about user defined data types, such as my Book object above
    you shouldn't have to use 'ref' to be able to change the original Book object. However, if Book were a struct then you would have to use 'ref' to change the original object because structs are classified with the primitive types in C#.
    Last edited by 7stud; 11-26-2005 at 06:05 AM.

  13. #28
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Wow, have I missed that all this time?!?!?! That example did work and I also created a simple Book example too.

    Book.cs
    Code:
    namespace Answers
    {
        public class Book
        {
            private int year;
    
            public int Year
            {
                get
                {
                    return this.year;
                }
                set
                {
                    this.year = value;
                }
            }
        }
    }
    Program.cs
    Code:
    public static void Main(string[] args)
    {
        Program p = new Program();
    
        StringBuilder y = new StringBuilder();
        y.Append("hello");
        p.Foo(y);
        Console.WriteLine(y);
        
        Book b = new Book();
        b.Year = 2005;
        Console.WriteLine(b.Year);
        p.ChangeYear(b);
        Console.WriteLine(b.Year);
    
        System.Console.ReadLine();
    }
    
    public void Foo(StringBuilder x)
    {
        x.Append(" world");
    }
    
    public void ChangeYear(Book b)
    {
        b.Year = 1999;
    }

  14. #29
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Wow, have I missed that all this time?!?!?!


    Now try this:
    Code:
    public void Foo(StringBuilder x)
    {
        x = null;
    }
    
    public static void Main(string[] args)
    {
        
        StringBuilder y = new StringBuilder();
        y.Append("hello");
        Foo(y);
        Console.WriteLine(y);
    }
    Explain the result.

    What does this do in C#?
    Code:
    Program p = new Program();

  15. #30
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Quote Originally Posted by 7stud
    Now try this...
    Weird, that just printed out "hello", so it didn't nullify the StringBuilder from above.

    Quote Originally Posted by 7stud
    What does this do in C#?
    Code:
    Program p = new Program();
    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.

Page 2 of 3 FirstFirst 123 LastLast
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, 02: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, 01:11 AM

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