参照渡し
C++ではポインタや参照型を使えば参照渡しができます。
C#では値型クラスの参照渡しにキーワードref,outを使います。
C++/CLIは%を使って参照渡しができますが、これはC#のrefに相当します。
C++/CLIにはoutに該当する構文がありませんが、
C#のモジュールとやりとりにはoutが表現できないと困ることがあります。
C++/CLIでoutを表現するには属性を使います。
C#では値型クラスの参照渡しにキーワードref,outを使います。
C++/CLIは%を使って参照渡しができますが、これはC#のrefに相当します。
C++/CLIにはoutに該当する構文がありませんが、
C#のモジュールとやりとりにはoutが表現できないと困ることがあります。
C++/CLIでoutを表現するには属性を使います。
| C# | C++/CLI | |
|---|---|---|
| ref | ref int x | int% x |
| out | out int x | [Runtime::InteropServices::Out] int% x |
[C#]
using System;
class Program
{
static void f(int normalInt, ref int refInt, out int outInt)
{
normalInt *= 10;
refInt *= 10;
outInt = 30;
}
static void Main()
{
int normalInt = 1;
int refInt = 2;
int outInt;
f(normalInt, ref refInt, out outInt);
Console.WriteLine("{0} {1} {2}", normalInt, refInt, outInt);
}
}
[出力]
1 20 30
[C++/CLI]
using namespace System;
void f(int normalInt, int% refInt,
[Runtime::InteropServices::Out] int% outInt)
{
normalInt *= 10;
refInt *= 10;
outInt = 30;
}
int main()
{
int normalInt = 1;
int refInt = 2;
int outInt;
f(normalInt, refInt, outInt);
Console::WriteLine("{0} {1} {2}", normalInt, refInt, outInt);
return 0;
}
[出力]
1 20 30