スポンサーサイト

上記の広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書く事で広告が消せます。

参照渡し

C++ではポインタや参照型を使えば参照渡しができます。
C#では値型クラスの参照渡しにキーワードref,outを使います。
C++/CLIは%を使って参照渡しができますが、これはC#のrefに相当します。
C++/CLIにはoutに該当する構文がありませんが、
C#のモジュールとやりとりにはoutが表現できないと困ることがあります。
C++/CLIでoutを表現するには属性を使います。

C#C++/CLI
refref int xint% x
outout 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

テーマ : プログラミング
ジャンル : コンピュータ

コメントの投稿

非公開コメント

プロフィール

Author:bear09
FC2ブログへようこそ!

最新記事
最新コメント
最新トラックバック
月別アーカイブ
カテゴリ
検索フォーム
RSSリンクの表示
リンク
Powered By FC2ブログ

今すぐブログを作ろう!

Powered By FC2ブログ

ブロとも申請フォーム

この人とブロともになる

QRコード
QR