30

I know that the C++/CLI code

void foo(Bar^% x);

transforms into

Void foo(ref Bar x);

What is the C++/CLI code that becomes

Void foo(out Bar x);

?

Alexandre C.
  • 55,948
  • 11
  • 128
  • 197

3 Answers3

42

You can use the OutAttribute:

using namespace System::Runtime::InteropServices;    
void foo([Out] Bar^% x); 
Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
8

There is no such specific syntax in C++/CLI. I think you can get fairly close by adding the OutAttribute to modify the parameter. But I'm not sure that achieves the exact same semantics as C# out.

The concept of out is for the most part limited to C#. The CLR really only sees ref parameters. The out concepts is achieved via a mod opt I believe and most languages ignore it.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 1
    It is the pass-by-reference syntax with `OutAttribute` applied to the parameter as you say. It creates the exact same metadata as with C# `out` parameters. By "not... the exact same semantics" are you referring to the fact that in C# the parameter starts out uninitialized and must be definitely assigned before the function returns? – Ben Voigt Aug 19 '10 at 02:37
0

"Sorry for my English" In C++ you have "pointers" for example:

int a = 0;
int *b = &a;  // '*' means that it's a pointer variable, '&' - give you a place in memory there has an object 'a'.
*b = 1; // '*' then if you want to get information which in this memory, you need to put the '*'.

Example for Functions

void Foo(int *pa) 
{
   (*pa)++;
}
void main()
{
   int a = 0;
   Foo(&a);
}

As you see it works like ref and out hotkeys in C#

Alegro
  • 1
  • 1