为什么我的静态布尔数组未正确初始化?只有第一个被初始化 - 我怀疑这是因为数组是静态的。静态Bool数组未初始化为集
下面的MWE是用GCC编译的,它基于一个函数,我写这个函数是为了说明我的问题而转入主程序。我曾尝试使用和不使用C++ 11。我的理解是因为这个数组是静态的,并且初始化为true,所以当我第一次进入我的函数时应该总是打印。所以在这个MWE中它应该打印一次。
#include <iostream>
using namespace std;
const int arraysize = 10;
const int myIndex = 1;
static bool firstTimeOverall = true;
int main()
{
static bool firstCloudForThisClient[arraysize] = {true};
cout.flush();
if (firstCloudForThisClient[myIndex])
{
cout << "I never get here" << endl;
firstCloudForThisClient[myIndex] = false;
if (firstTimeOverall)
{
firstTimeOverall = false;
cout << "But think I would get here if I got in above" << endl;
}
}
return 0;
}
是什么让你认为firstCloudForThisClient'的'所有元素都被初始化为'真'? – quamrana