passing & filling string class variables

G

Guest

I have a function that passes a string class pointer to a function, this
function is then suppose to fill it and the outer function uses it. But I
believe that I am running into problem due to manged memory. Because it has
value in inner function, but does not have the assigned value in outher
function. So I need to know how to declare this variable and make
assignments correctly so that it will have value when it is back in outher
function: Below is basically how I am currently calling.
I am using .NET 2003 C++

func1 () {
String *empcode;

empcode = new String ("0");
func2(empcode)
}

func2(String *empcode) {
empcode = "empcode value";
}

I have tried it with and without the new String("0") line in func1

Thangs for your help
 
D

David Wilkinson

brian_harris said:
I have a function that passes a string class pointer to a function, this
function is then suppose to fill it and the outer function uses it. But I
believe that I am running into problem due to manged memory. Because it has
value in inner function, but does not have the assigned value in outher
function. So I need to know how to declare this variable and make
assignments correctly so that it will have value when it is back in outher
function: Below is basically how I am currently calling.
I am using .NET 2003 C++

func1 () {
String *empcode;

empcode = new String ("0");
func2(empcode)
}

func2(String *empcode) {
empcode = "empcode value";
}

I have tried it with and without the new String("0") line in func1

Thangs for your help

Brian:

In the CLI object model zoo, System::String is that special beast: the
immutable reference class.

I do not fully understand why a supposedly simpler language needs these
different constructs. C++ is so clean in this regard: every type
(intrinsic or user-defined) can be created on the stack or on the heap,
and every object can be passed by value, pointer or reference. No
exceptions.

David Wilkinson


David Wilkinson
 
G

Guest

As I understand it when you do an assignment it is suppose to delete old
string and create new string. The value for the empcode variable is correct
in func2. So I think it is more an issue of how to pass parameter by
reference correctly. Or maybe it is due to the auto managing and it is just
freeing the value I want to keep when it exits func2.
 
D

David Wilkinson

brian_harris said:
As I understand it when you do an assignment it is suppose to delete old
string and create new string. The value for the empcode variable is correct
in func2. So I think it is more an issue of how to pass parameter by
reference correctly. Or maybe it is due to the auto managing and it is just
freeing the value I want to keep when it exits func2.

Brian:

I'm only just starting on managed code (and I'm learning C++/CLI) but I
think you have to do

func2(String*& empcode)
{
empcode = "empcode value";
}

David Wilkinson
 
G

Guest

Brian.... I only know C# and I am a newbie to C++/cli. So I can only answer
in C++/cli. In SomeMethod(SomeObject^ obj) a handle of type SomeObject is
passed and a copy of the handle goes on the stack. On method entry both
handles, the original and the copy "point" to same string on the heap. You
can reassign another string literal to the copy of the handle on the stack
within the method, but when the method exits the copy of the handle is popped
off the stack and the new string literal may be eligible for garbage
collection. The original handle still "points" to the original string. If you
want to get a string value just do:

String^ GetString() {
return L"New Value";
}

and call it as:

String^ someString= someInstanceHandle->GetString();

If you actually want to do a swap routine, you need to pass a handle by
reference as the following code demonstrates:

#include "stdafx.h"

using namespace System;

public ref class Swap {
public:
static void SwapByValue(String^ s1, String^ s2) {
String^ temp= s1;
s1= s2;
s2= temp;
}
static void SwapByRef(interior_ptr<String^> s1, interior_ptr<String^> s2) {
String^ temp= *s1;
*s1= *s2;
*s2= temp;
}
static String^ GetString() {
return L"New Value";
}
};

int main(array<System::String ^> ^args)
{
String^ s1= L"One";
String^ s2= L"Two";
Swap::SwapByValue(s1,s2);
Console::WriteLine(s1); // -> one
Swap::SwapByRef(&s1,&s2);
Console::WriteLine(s1); // --> two
Console::WriteLine(s2); // --> one
String^ someString= Swap::GetString();
Console::WriteLine(someString);
Console::ReadLine();
return 0;
}
 
G

Guest

David... Yup.. I got that to compile as SomeFunction(String^ & s1) which I
believe is the equivalent to C# SomeFunction(String ref s1) or pass by
reference.
 
G

Guest

Thanks, that worked for me in C++. I don't recognize that syntax as valid,
since I just changed my declartion to use what you specified and did not need
to change anything comming into function or inthe use of that variable inside
function. So do you have some book that is good at explaning what is leagal
in managed C++ and list of classes. I have been using visual C++ .net step
by step from microsoft for version 2003. While it has given me enogh
information to get started it leaves out alot of stuff.
thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top