Can you print anything in C++, before entering into the main function?

前端 未结 4 1971
Happy的楠姐
Happy的楠姐 2020-12-09 12:53

Can you print anything in C++, before entering into the main function?

It is interview question in Bloomberg:

Answer :create a global varia

4条回答
  •  有刺的猬
    2020-12-09 13:25

    Header file

    class A
    {
       static A* a;
    public:
       A() { cout << "A" ; }
    };
    

    Implementation file:

    A* A::a = new A;
    

    Well, statics (and not only) are initialized before the call to main.

    EDIT

    Another one:

    bool b = /*(bool)*/printf("before main");
    
    int main()
    {
       return 0;
    }
    

提交回复
热议问题