What is a static constructor?

后端 未结 13 1875
天涯浪人
天涯浪人 2020-11-27 03:46

This question was asked to me in an interview:

What is a static constructor?

Does it exist in C++? If yes, please explain it wit

13条回答
  •  青春惊慌失措
    2020-11-27 04:07

    Here is the simplest static constructor in c++17:

    #include 
    class h {
        public:
        static inline int i;
        private:
        struct constructor {
            constructor() {
             i=3;
            }
        };
        static inline constructor cons;
    };
    
    int main() {
      std::cout << h::i;
    }
    

提交回复
热议问题