C++ C++;初始化静态数组

C++ C++;初始化静态数组,c++,arrays,static,C++,Arrays,Static,我甚至不确定这是否可能。从我所看到的所有示例中,数组是在{}括号中定义的,但在我的例子中,这不太可能 我想做的是在我的绘图函数中保留这个,以绘制多个慢慢增大的圆 通过使用调试器,我得到的是每次循环命中时都会重置静态数组 我也尝试过类似于staticrectrc[5]={} void fun_called_every_five_seconds() { static Rect rc[5]; for (int i = 0; i < count; i++) {

我甚至不确定这是否可能。从我所看到的所有示例中,数组是在
{}
括号中定义的,但在我的例子中,这不太可能

我想做的是在我的绘图函数中保留这个,以绘制多个慢慢增大的圆

通过使用调试器,我得到的是每次循环命中时都会重置静态数组

我也尝试过类似于
staticrectrc[5]={}

void fun_called_every_five_seconds() {
     static Rect rc[5];

     for (int i = 0; i < count; i++) {
         int x = rand()%400;
         int y = rand()%400;
         int r = rand()%200;
         rc[i] = Rect (x,y,r,r);
     }

     rc[0].X += 50;


     // I check value of rc[0].X right here
 }
void fun\u每五秒调用一次(){
静态Rect-rc[5];
for(int i=0;i
每次循环命中时都会重置静态数组

是的,循环显式重置静态数组

最小的更改是只运行一次初始化循环:

void for_fun_called_every_five_seconds() {
    static Rect rc[5];
    static bool init = false;
    if (!init) { // this only happens once
        init = true;
        for (int i = 0; i < count; i++) {
            int x = rand()%400;
            int y = rand()%400;
            int r = rand()%200;
            rc[i] = Rect (x,y,r,r);
        }
    }

    rc[0].X += 50;

    // I check value of rc[0].X right here
}
void for_fun_每五秒调用一次(){
静态Rect-rc[5];
静态bool init=false;
如果(!init){//这只发生一次
init=真;
for(int i=0;i
但这是相当丑陋的,不必要的难以解释。喜欢像这样的东西

class Circles {
    Rect rc[5];
public:
    Circles() {
        for (int i = 0; i < count; i++) {
            int x = rand()%400;
            int y = rand()%400;
            int r = rand()%200;
            rc[i] = Rect (x,y,r,r);
        }
    }
    void for_fun_called_every_five_seconds() {
        // should this also be in a loop,
        // ie. each rc[i] gets increased?
        rc[0].X += 50;
        // I check value of rc[0].X right here
    }
};
类圆{
Rect-rc[5];
公众:
圆圈(){
for(int i=0;i
每次循环命中时都会重置静态数组

是的,循环显式重置静态数组

最小的更改是只运行一次初始化循环:

void for_fun_called_every_five_seconds() {
    static Rect rc[5];
    static bool init = false;
    if (!init) { // this only happens once
        init = true;
        for (int i = 0; i < count; i++) {
            int x = rand()%400;
            int y = rand()%400;
            int r = rand()%200;
            rc[i] = Rect (x,y,r,r);
        }
    }

    rc[0].X += 50;

    // I check value of rc[0].X right here
}
void for_fun_每五秒调用一次(){
静态Rect-rc[5];
静态bool init=false;
如果(!init){//这只发生一次
init=真;
for(int i=0;i
但这是相当丑陋的,不必要的难以解释。喜欢像这样的东西

class Circles {
    Rect rc[5];
public:
    Circles() {
        for (int i = 0; i < count; i++) {
            int x = rand()%400;
            int y = rand()%400;
            int r = rand()%200;
            rc[i] = Rect (x,y,r,r);
        }
    }
    void for_fun_called_every_five_seconds() {
        // should this also be in a loop,
        // ie. each rc[i] gets increased?
        rc[0].X += 50;
        // I check value of rc[0].X right here
    }
};
类圆{
Rect-rc[5];
公众:
圆圈(){
for(int i=0;i
另一个答案很好,只要整个过程不必是线程安全的

如果初始化必须是线程安全的,那么除了将所有内容都压入声明之外,别无选择

但是,可以使用助手类来最小化键入:

class RandomRect : public Rect {

public:
    RandomRect() : RandomRect(rand() % 400, rand() % 400, rand % 200) {}
    RandomRect(int x, int y, int r) : Rect(x, y, r, r) {}
};

// ... Then, initialize the static array as follows:

static Rect rc[5]={RandomRect(), RandomRect(), RandomRect(),
                   RandomRect(), RandomRect()};

另一个答案很好,只要整个过程不必是线程安全的

如果初始化必须是线程安全的,那么除了将所有内容都压入声明之外,别无选择

但是,可以使用助手类来最小化键入:

class RandomRect : public Rect {

public:
    RandomRect() : RandomRect(rand() % 400, rand() % 400, rand % 200) {}
    RandomRect(int x, int y, int r) : Rect(x, y, r, r) {}
};

// ... Then, initialize the static array as follows:

static Rect rc[5]={RandomRect(), RandomRect(), RandomRect(),
                   RandomRect(), RandomRect()};

这没什么好惊讶的。每次调用一个函数时,它的所有代码都从它的参数执行。 这里唯一的例外是
静态
变量的初始化,该变量保证在程序生命周期内只运行一次(自C++11以来也是线程安全的)。 变量的初始化只在其定义过程中发生,在您在此上下文中为变量命名的那一行。之后的所有代码都只是简单地“修改”变量,以给它您想要的值:它不是初始化

这里有两种解决方案。或者使用带有lambda的
std::call_once
(请参阅),使“初始化”代码只运行一次。 或者在变量定义的一行中放入所有代码。对于C样式的数组来说可能很复杂,但对于
std::array
就不复杂了,它可以很容易地从函数返回并用于初始化
std::array

std::array<Rect, 5> initialize_rc() {
   std::array<Rect, 5> rc;

   for (int i = 0; i < count; i++) {
       int x = rand()%400;
       int y = rand()%400;
       int r = rand()%200;
       rc[i] = Rect (x,y,r,r);
   }

   return rc;
}

void for_fun_called_every_five_seconds() {
   static std::array<Rect, 5> rc = initialize_rc();

   rc[0].X += 50;

   // I check value of rc[0].X right here
}
std::数组初始化\u rc(){
std::数组rc;
for(int i=0;i
这里没有什么值得惊讶的。每次调用一个函数时,它的所有代码都从它的参数执行。 这里唯一的例外是
静态
变量的初始化,该变量保证在程序生命周期内只运行一次(自C++11以来也是线程安全的)。 变量的初始化只在其定义过程中发生,在您在此上下文中为变量命名的那一行。之后的所有代码都只是简单地“修改”变量,以给它您想要的值:它不是初始化

这里有两种解决方案。或者使用带有lambda的
std::call_once
(请参阅),使“初始化”代码只运行一次。 或者在变量定义的一行中放入所有代码。对于C样式的数组来说可能很复杂,但对于<