0

练习吃豆人

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 个“假”的数组。为什么 ?

4

2 回答 2

1

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;
于 2012-09-03T16:15:31.503 回答
1

或者只是将 for each 循环更改为使用引用,例如 for each(aEats 中的 bool% b)。——本·沃伊特

于 2012-09-14T19:56:44.493 回答