What is the rationale for not having static constructor in C++?

后端 未结 5 1541
长情又很酷
长情又很酷 2020-12-24 12:38

What is the rationale for not having static constructor in C++?

If it were allowed, we would be initializing all the static members in it, at one place in a very org

5条回答
  •  猫巷女王i
    2020-12-24 12:53

    This doesn't really make sense for c++ - classes are not first class objects (like in e.g. java).

    A (static|anything) constructor implies something is constructed - and c++ classes aren't constructed, they just are.

    You can easily achieve the same effect though:

    //.h
    struct Foo {
      static std::vector strings;
    };
    //.cpp
    std::vector Foo::strings(createStrings());
    

    IMO there's just no need for one more syntactic way of doing this.

提交回复
热议问题