ref Class and a value by reference

Z

zon7

Hi, what i'm trying to do is obtaining a value from a function in a ref
class.
In this context I can't use the "ref" and "out" operators.
I have tried all i can imagine but still hasn't it working.
With an array it works great.

void Change(int a, array<int>^ m)
{
m[0]=a;
}

With this code, in the function that calls Change the value will be
changed.
But with an int it doesnt work


void Change(int a, int^ m)
{
m=a;
}

Any guess?
 
M

Mattias Sjögren

void Change(int a, int^ m)
{
m=a;
}

Any guess?

My guess is that you posted to the wrong newsgroup. This one is for
C#, not C++. But the C++/CLI reference parameter syntax is, IIRC

void Change(int a, int^% m)


Mattias
 
A

Alan Pretre

Mattias Sjögren said:
My guess is that you posted to the wrong newsgroup. This one is for
C#, not C++. But the C++/CLI reference parameter syntax is, IIRC

void Change(int a, int^% m)

Actually the syntax in C++ would be

void Change(int a, int& m) {}

-- Alan
 
G

Guest

In C++/CLI:
^% is for passing reference types by ref.
% is for passing value types by ref.
e.g.,
passing by ref:
void test(int %myInt, System::Object ^%myObject)

vs passing by value:
void test(int myInt, System::Object ^myObject)
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
 
A

Alan Pretre

David Anton said:
In C++/CLI:
^% is for passing reference types by ref.
% is for passing value types by ref.
e.g.,
passing by ref:
void test(int %myInt, System::Object ^%myObject)

vs passing by value:
void test(int myInt, System::Object ^myObject)

David, why doesn't int& work?

-- Alan
 
G

Guest

That works fine also. I posted before I saw your post so I wasn't implying
yours was wrong.
Our converters use % to be consistent with the style of passing managed
reference types by ref. It's easier for some people to understand that "%"
means by ref, regardless of whether it's a value type or a reference type
(where it's combined with the 'hat').
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
 
W

Willy Denoyette [MVP]

I have to disagree, int& doesnt work in C++/CLI, the correct syntax is int%
as you mentioned before.

Willy.

| That works fine also. I posted before I saw your post so I wasn't
implying
| yours was wrong.
| Our converters use % to be consistent with the style of passing managed
| reference types by ref. It's easier for some people to understand that
"%"
| means by ref, regardless of whether it's a value type or a reference type
| (where it's combined with the 'hat').
| --
| David Anton
| www.tangiblesoftwaresolutions.com
| Instant C#: VB to C# converter
| Instant VB: C# to VB converter
| Instant C++: C#/VB to C++ converter
| C# Code Metrics: Quick metrics for C#
|
|
| "Alan Pretre" wrote:
|
| > | > > In C++/CLI:
| > > ^% is for passing reference types by ref.
| > > % is for passing value types by ref.
| > > e.g.,
| > > passing by ref:
| > > void test(int %myInt, System::Object ^%myObject)
| > >
| > > vs passing by value:
| > > void test(int myInt, System::Object ^myObject)
| >
| > David, why doesn't int& work?
| >
| > -- Alan
| >
| >
| >
 
W

Willy Denoyette [MVP]

| | > In C++/CLI:
| > ^% is for passing reference types by ref.
| > % is for passing value types by ref.
| > e.g.,
| > passing by ref:
| > void test(int %myInt, System::Object ^%myObject)
| >
| > vs passing by value:
| > void test(int myInt, System::Object ^myObject)
|
| David, why doesn't int& work?
|


David is right, passing value types by ref must use %.
This is managed C++/CLI (managed code using /clr compileroption) compiler
syntax not C++/ISO, & if for unmanaged code (or mixed) only.

Try to compile this using cl /LD /clr ....
#using <system.dll>

using namespace System;
using namespace System::Collections::Generic;

public ref class MyClass
{
public:
void TestFunc2(int& o) {...}
};

you'll get :
error C4956: 'int &' : this type is not verifiable
.... for the function argument o.

While this:
....
public ref class MyClass
{
public:
void TestFunc2(int& o)
{}
};
#pragma unmanaged
void TestFunc2(int& o) {...}

will pass when compiled with /clr, but produces unverifiable code, it won't
compile at all using clr:pure or /clr:safe.



Willy.
 
A

Alan Pretre

Willy Denoyette said:
David is right, passing value types by ref must use %.
This is managed C++/CLI (managed code using /clr compileroption) compiler
syntax not C++/ISO, & if for unmanaged code (or mixed) only.

will pass when compiled with /clr, but produces unverifiable code, it
won't
compile at all using clr:pure or /clr:safe.


I see what you are saying... but I don't see the same thing with this
program in VS.NET 2005,

#include "stdafx.h"
#include "stdio.h"

using namespace System;

static void Change(int& m) {
m = 3;
return;
}

int main(array<System::String ^> ^args) {
int m = 1;
Change(m);
printf("%d\n", m);
return 0;
}


These two project settings compile cleanly with no warnings...
Common Language Runtime Support (/clr)
Pure MSIL Common Language Runtime Support (/clr:pure)

While this one gives 466 errors, probably int& is in their somewhere, but so
is everything + the kitchen sink.
Safe MSIL Common Language Runtime Support (/clr:safe)


-- Alan
 
G

Guest

Thanks for the clarification Willy.
I know we did a technical evaluation of the proper C++/CLI parameter
protocol for our converters, and I recall now that the use of % for passing
value types by ref was not merely for consistency purposes with the passing
of reference types by ref.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
 
W

Willy Denoyette [MVP]

| | > David is right, passing value types by ref must use %.
| > This is managed C++/CLI (managed code using /clr compileroption)
compiler
| > syntax not C++/ISO, & if for unmanaged code (or mixed) only.
|
| <snip>
|
| > will pass when compiled with /clr, but produces unverifiable code, it
| > won't
| > compile at all using clr:pure or /clr:safe.
|
|
| I see what you are saying... but I don't see the same thing with this
| program in VS.NET 2005,
|
| #include "stdafx.h"
| #include "stdio.h"
|
| using namespace System;
|
| static void Change(int& m) {
| m = 3;
| return;
| }
|
| int main(array<System::String ^> ^args) {
| int m = 1;
| Change(m);
| printf("%d\n", m);
| return 0;
| }
|
|
| These two project settings compile cleanly with no warnings...
| Common Language Runtime Support (/clr)
| Pure MSIL Common Language Runtime Support (/clr:pure)
|
| While this one gives 466 errors, probably int& is in their somewhere, but
so
| is everything + the kitchen sink.
| Safe MSIL Common Language Runtime Support (/clr:safe)
|

Yes, but while your code compiles to MSIL (option pure) without a warning,
it generates unverifiable code, that means it cannot be called from pure
managed language code like C# (even not using PInvoke), native pointers
(int&) are non verifiable, really.

Try running peverify on the produced assembly Compiled with /clr, you'll get
a Unverifiable PE header error..., compile it with 'pure' as option and
watch the error list scroll of the screen when running PEverify. That means
that these assemblies are standalone non callable from anything else than
C++/CLI.

My code compiles to mixed code (the one with the #pragma unmanaged) which is
unverifiable by nature (the PE header indicates this), but it's still
callable from C# (the managed portion only though.

Note also, a ref class member function cannot use int&, the correct syntax
is int%.

Willy.
 
W

Willy Denoyette [MVP]

| Thanks for the clarification Willy.
| I know we did a technical evaluation of the proper C++/CLI parameter
| protocol for our converters, and I recall now that the use of % for
passing
| value types by ref was not merely for consistency purposes with the
passing
| of reference types by ref.

That's the right decission, & in reality denotes a native pointer, which are
non verifiable attributes, it should not be used in managed code.

Willy.
 
A

Alan Pretre

OK thanks.

I made the leap to C# four years ago, they've added a bit since I last
worked in C++. Can't say that I miss it.

-- Alan
 
W

Willy Denoyette [MVP]

| OK thanks.
|
| I made the leap to C# four years ago, they've added a bit since I last
| worked in C++. Can't say that I miss it.

Well, I find C++/CLI quite attractive, but there is still a lot to do at the
level of type unification, don't know if the C++ team still cares that much
about this.. Guess they are now busy with bug solving and to get the
stl/clr stuff right for the Orcas release.


Willy.
 
Z

zon7

Ok. thanks a lot. It was the % character.
I really didn't know it existed :p
I have to take a look at C++/CLI i think
 

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