Are C ++ static simple types initialized in order?

My experience tells me that this object:

class Object
{
private:
    static int array[];

public:
    Object(int id);
};


int Object::array[] = { 2937, 892 };


Object::Object(int id)
{
    // do something
}

Initialization arraywill occur before calling any method on Objector calling any method on any other object in the program, whether the object is declared staticor not.

Basically, I ask, who does not agree with the fact that the static simple types of C (not objects), such as char, short, intand long(and structure without the designers, made up of these types) are initialized when an executable file is loaded into memory before calling main () or any other constructor?

+3
source share
5 answers
+7

, , , :

, , - , C (-), char, short, int long ( , ), , , main() ?

, , - . , , , :

  • 3.6.2

(3.7.1) (8.5) . ; . POD (3.9) (5.19) . , .

,

static int x = getvalue();

, ( ).

, ( , ), , , .

+7

. , , - , .

+4

, , , ( , ).

I think any decent compiler will do this. Then, when your exe is loaded into memory, it already has hardcoded values.

+2
source

The standard only says that static is initialized before control enters the area in which they can be accessed. He does not define anything. I think file-region variables are a completely different thing, but generally using statics and depending on static behavior is a bad idea.

0
source

Source: https://habr.com/ru/post/1769824/


All Articles