passing and returning arguments to functions

for the life of me i dont know how to make this work in Managed C++. what am i doing wrong

ref class Test

{

public:

static void testFunc(String^ buffer)

{

buffer = "altered";

}

};

int main(array<System::String ^> ^args)

{

Test^ testClass = gcnew Test();

String^ buf = gcnew String("some string");

Console::WriteLine("before {0}", buf); //should print "some string"

testClass->testFunc(buf);

Console::WriteLine("after {0}", buf); //should print "altered"

return 0;

}



Answer this question

passing and returning arguments to functions

  • magcianaux

    thank you for that correction! it makes it easier when i do a search on google for help.
  • jwraith

    Just a small correction, the language you are using is no longer called Managed (Extensions to) C++, but C++/CLI.

  • Greg J. Brown

    You need to pass the handle to the String by reference

    Aaron Sulwer wrote:

    for the life of me i dont know how to make this work in Managed C++. what am i doing wrong

    ref class Test

    {

    public:

    static void testFunc(String^% buffer)

    {

    buffer = "altered";

    }

    };

    int main(array<System::String ^> ^args)

    {

    Test^ testClass = gcnew Test();

    String^ buf = gcnew String("some string");

    Console::WriteLine("before {0}", buf); //should print "some string"

    testClass->testFunc(buf);

    Console::WriteLine("after {0}", buf); //should print "altered"

    return 0;

    }

    This is similar to what you would do in ISO/IEC C++ with std::string*&



  • passing and returning arguments to functions