Visual C ++ / CLI中托管数组或托管对象数组

时间:2012-09-03 14:07:01

标签: arrays c++-cli managed

练习PacMan

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

2 个答案:

答案 0 :(得分:1)

bool 类型是一种值类型,您在for-each语句中获得值的副本,而不是引用。因此,您将副本设置为true,这不会传播回数组元素。请改用简单的for循环:

Etats = gcnew array<bool>(100);
for (int ix = 0; ix < Etats->Length; ++ix)
    Etats[ix] = true;

答案 1 :(得分:0)

或者只是更改for循环以使用引用,例如对于每个(aEats中的bool%b)。 - Ben Voigt