C++11 静态布尔数组未按设置初始化

C++11 静态布尔数组未按设置初始化,c++11,static,initialization,array-initialization,C++11,Static,Initialization,Array Initialization,为什么我的静态布尔数组没有正确初始化?只有第一个被初始化-我怀疑这是因为数组是静态的 下面的MWE是用GCC编译的,它基于我正在编写的一个函数,我已经将该函数转换到一个主程序中来说明我的问题。我试过使用和不使用c++11。我的理解是,因为这个数组是静态的,并且初始化为true,所以在我第一次输入函数时,应该始终打印它。所以在这个MWE中,它应该打印一次 #include <iostream> using namespace std; const int arraysize = 1

为什么我的静态布尔数组没有正确初始化?只有第一个被初始化-我怀疑这是因为数组是静态的

下面的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;
}
#包括
使用名称空间std;
常数int arraysize=10;
常数int myIndex=1;
静态bool firsttimetotal=true;
int main()
{
静态bool firstCloudForThisClient[arraysize]={true};
cout.flush();
if(firstCloudForThisClient[myIndex])
{

cout您正在使用
array[size]={true}
初始化数组上的第一个元素,如果arraysize变量大于1,则其他元素的初始值取决于平台。我认为这是一种未定义的行为

如果确实需要初始化数组,请改用循环:

for(int i=0; i < arraysize; ++i)
firstCloudForThisClient[i] = true;
for(int i=0;i
您可能需要反转条件以利用默认初始化:

#include <iostream>

using namespace std;

const int arraysize = 10;
const int myIndex = 1;  // note this index does not access the first element of arrays

static bool firstTimeOverall = true;

int main()
{
    static bool firstCloudForThisClient[arraysize] = {}; // default initialise
    cout.flush();
    if (!firstCloudForThisClient[myIndex])
    {
        cout << "I never get here" << endl;
        firstCloudForThisClient[myIndex] = true; // Mark used indexes with true
        if (firstTimeOverall)
        {
            firstTimeOverall = false;
            cout << "But think I would get here if I got in above" << endl;
        }
    }
    return 0;
}
#包括
使用名称空间std;
常数int arraysize=10;
const int myIndex=1;//注意此索引不访问数组的第一个元素
静态bool firsttimetotal=true;
int main()
{
静态bool firstCloudForThisClient[arraysize]={};//默认初始化
cout.flush();
如果(!firstCloudForThisClient[myIndex])
{
库特
这会将第一个条目初始化为true,将所有其他条目初始化为false

if (firstCloudForThisClient[myIndex])

但是,由于
myIndex
是1,数组索引是从零开始的,因此这将访问第二个条目,该条目为false。

您应该访问数组的第一个元素,因此使用:

const int myIndex = 0;

是什么让您认为该客户端的所有元素都被初始化为
true
?因为这是一个静态变量,用于一个被多次调用的函数,我不能使用循环-这是否意味着我应该将此函数转换为对象?是的,将此功能转换为对象可能是有用的对象。或者,您可以将此循环移动到其他函数中并调用一次,例如在程序开始时。如果您非常喜欢静态变量,您甚至可以创建一个静态bool标志,以保护您再次初始化数组。感谢您的建议-我正在playinf尝试获得一些更复杂的线程代码来使用func来自点云库的可选性。出于这个原因,我现在接受你的后一个建议,以后再接受你的前一个建议(将其转换为对象)。谢谢!@GoverNator数组上的列表初始值设定项按顺序使用给定的初始值设定项,值初始化其余的项,这意味着所有的基元都为0(即bools为false)。它不是未定义的,也不依赖于平台。@user3235290,因为答案不正确,正如我在上面的评论中解释的那样。
const int myIndex = 0;