Suppose we have a class in C++
class X {
int i;
public:
X(int y): i(y) {}
};
X r(10);
int main() {
return 0;
}
So we have an global object r.
My question is that when the constructor for global or static objects is been called, at compile-time or at runtime? Because the execution of the program starts at the main function.
And till then all the global objects should be intitialized.
Certainly not at compile time. It’s called at run-time, before
mainenters. All globals and statics (excluding method scoped statics) are initialized then.