none
String by ref i C++ old syntax RRS feed

  • Question

  • Hi,
     I have one question: is it possible to write C++ code with equal functionality using System.String and /clr oldsyntax in VS2005(C++/CLI) like code below? If yes - pls join example. If no - pls join substitution. (Main problem for me is getting String by ref with Func1)
    1     class Program  
    2     {  
    3         static void Func1(ref String s)  
    4         {  
    5             s = "Changed";  
    6         }  
    7  
    8         static void Main(string[] args)  
    9         {  
    10             string s = "Nothing";  
    11             Console.WriteLine(s);  
    12             Func1(ref s);  
    13             Console.WriteLine(s);  
    14             Console.ReadKey();  
    15         }  
    16     }  
    17  

    THX for replies!!!
    Thursday, August 28, 2008 5:04 PM

Answers

  • void Func1(String **s)  
    {  
      *s = S"Changed";  
    }  
     

    Mattias, C# MVP
    • Marked as answer by Zanthar Friday, September 5, 2008 11:12 AM
    Friday, August 29, 2008 7:19 AM
    Moderator
  • Not sure I understand the question, but there's nothing "unsafe" about the code.

    The end result should be the same both for String** and String*&

    Mattias, C# MVP
    • Marked as answer by Zhi-Xin Ye Wednesday, September 3, 2008 8:52 AM
    Tuesday, September 2, 2008 5:46 AM
    Moderator

All replies

  • I only know "new" syntax:

    #include "stdafx.h"

    using namespace System;

    void Func(String^% a) {
        a = "Changed";
    }

    int main(array<System::String ^> ^args)
    {
        String^ bla;
        Func(bla);
        Console::WriteLine(bla);
        Console::ReadLine();
        return 0;
    }

    Post to the C++ Language forum.

    Hans Passant.
    Friday, August 29, 2008 1:21 AM
    Moderator
  • void Func1(String **s)  
    {  
      *s = S"Changed";  
    }  
     

    Mattias, C# MVP
    • Marked as answer by Zanthar Friday, September 5, 2008 11:12 AM
    Friday, August 29, 2008 7:19 AM
    Moderator
  • Mattias Sjögren said:

    void Func1(String **s)  
    {  
      *s = S"Changed";  
    }  
     

    Mattias, C# MVP



    Is it safe becouse of garbage collector?
    And is there any difference between this code:
    void Func1(String *&s)  
    {  
      s = S"Changed";  
    Monday, September 1, 2008 12:44 PM
  • Not sure I understand the question, but there's nothing "unsafe" about the code.

    The end result should be the same both for String** and String*&

    Mattias, C# MVP
    • Marked as answer by Zhi-Xin Ye Wednesday, September 3, 2008 8:52 AM
    Tuesday, September 2, 2008 5:46 AM
    Moderator
  • THX!
    Friday, September 5, 2008 11:13 AM