练习吃豆人
array<bool>^ aEtats; //declared: an array of true/false states
aEtats = gcnew array<bool>(100); //this array will correspond with an array of "Pills"
for each (bool b in aEtats)
b=true;
我得到一个包含 100 个“假”的数组。为什么 ?
The bool type is a value type, you get a copy of the value in the for-each statement, not a reference. So you are setting the copy to true, this doesn't propagate back to the array element. Use a simple for loop instead:
Etats = gcnew array<bool>(100);
for (int ix = 0; ix < Etats->Length; ++ix)
Etats[ix] = true;
或者只是将 for each 循环更改为使用引用,例如 for each(aEats 中的 bool% b)。——本·沃伊特