Home > Blockchain >  Question about constructor called but not declared c
Question about constructor called but not declared c

Time:11-05

I have the following code:

#include <string>
#include <iostream>
using namespace std;
class S {
public:
    string name_;
    S(string const& name) : name_(name) { cout << "create " << name << endl; }
    ~S() { cout << "destroy " << name_ << endl; }
};

S one("one");

int main() {
    S two("two");
    //eleven.name_; error 'eleven' was not declared in this scope
}

S eleven("eleven");

which gets output:

create one
create eleven
create two
destroy two
destroy eleven
destroy one

I was wondering that:

How that eleven's constructor called before two's constructor?

Why eleven is not declared while its constructor was called?

CodePudding user response:

The one and eleven are constructed before the main is invoked. The main function doesn't have information about eleven, due to the main is declared before the eleven.

CodePudding user response:

The objects named one and eleven have static storage duration. And according to intialization rules

All non-local variables with static storage duration are initialized as part of program startup, before the execution of the main function begins.

Also note:

After all static initialization is completed, dynamic initialization of non-local variables occurs in the following situations:

Ordered dynamic initialization, which applies to all other non-local variables: within a single translation unit, initialization of these variables is always sequenced in exact order their definitions appear in the source code.

So in your code both objects named one and eleven are dynamically initialized in the order in which they appear in this particular translation unit according to the above quoted statement. Now since we have S one("one"); first, the constructor will be called for this object and you get create one as output. Then for S eleven("eleven"); constructor will be called and you get create eleven as output. Lastly the object inside main() is initialized. So the object named two is initialized and you get a print statement corresponding to this third object(named two).

Finally, remember that the objects are destroyed in opposite order so you get the output you observed.

Summary

create one          //dynamically initialized first
create eleven       //dynamically initalized second(that is after object one)
create two          //this is initialized third
destroy two         //this will be destroyed first because it was created last
destroy eleven      //this will be destroyed second because it was created second
destroy one         //this will be destroyed third because it was created first

You asked:

How that eleven's constructor called before two's constructor?

eleven's constructor was called becore two's constructor because eleven is a non-local variable with static storage duration and according to the very first quoted statement in my answer it will be initialized before the execution of main() function which contains two.

CodePudding user response:

global object are always consructed before "local object" if this answer you question. and you need to declare eleven before main function.

CodePudding user response:

How that eleven's constructor called before two's constructor and why eleven is not declared while its constructor was called.

referring to [basic.start.init] (draft n3337):

Non-local variables with static storage duration are initialized as a consequence of program initiation.

And since S eleven("eleven"); is a global variable, it has static storage duration and will therefore be initialized at program initiation, before entering into the main function. That is why you see it being initialized before S two("two");

  •  Tags:  
  • c
  • Related