最近、XNA で作成された単純なゲームの優れたテンプレートであるGame State Management (詳細: create.msdn.com/en-US/education/catalog/sample/game_state_management ) を使い始めました。
私は数日間その実装を分析してきましたが、このメソッドのLoadingScreen.csに疑いがあります。
/// <summary>
/// The constructor is private: loading screens should
/// be activated via the static Load method instead.
/// </summary>
private LoadingScreen(ScreenManager screenManager, bool loadingIsSlow,
GameScreen[] screensToLoad)
{
this.loadingIsSlow = loadingIsSlow;
this.screensToLoad = screensToLoad;
TransitionOnTime = TimeSpan.FromSeconds(0.5);
}
参照の代入がある理由がわかりません: this.screensToLoad = screensToLoad;
。.Clone()
代わりにメソッド
のようなものを使用しないのはなぜですか?
[編集]
わかりました... 私の問題は XNA や Game State Management ではないと思います。私が疑問に思っていることを説明するコードを用意しました。
コードは次のとおりです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test
{
public class a
{
public int number = 3;
}
public class b
{
public a tmp;
public void f(a arg)
{
tmp = arg; // (*?*) isn't it dangerous assigning?
}
}
class Program
{
static void Main(string[] args)
{
b temp_b = new b();
{// (*!*) CODE BLOCK COMES HERE:
a temp_a = new a();
temp_b.f(temp_a);
temp_a.number = 4;
}
// TROUBLE EXPLANATION:
// We are outside of code block which I marked with (*!*)
// Now reference 'temp_a' is inaccessible.
// That's why line of code which I marked with (*?*) is dangerous.
// We saved 'temp_a' which is no longer accessible in 'temp_b' object.
//
// Now computer's memory pointed by reference, which is saved in 'temp_b.tmp' (and was saved in 'temp_a'),
// can be overriden by code which comes somewhere below this comment (or even in another thread or process).
//
// I think the same situation is in XNA GSM's piece of code.
// The only solution in my opinion is deep copy (AFAIK .Clone() can be implemented as shallow or deep copy).
Console.WriteLine(temp_b.tmp.number); // result is 4
// because we copied reference
// For me it's strange that this line was printed. As I mentioned above
// memory intended for 'temp_a' could be reused and overwritten.
}
}
}
便宜上、ここに同じコードを示します: ideone.com/is4S3。
上記のコードに質問と疑問を入れました(コメントを参照)。