What is a static constructor?

后端 未结 13 1858
天涯浪人
天涯浪人 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

    Other way to emulate static constructor behaviour is to use instance variable with private constructor and static factory method.

    Cat* Cat::give_birth() {
      static Cat *myone = NULL;
      if (myone == NULL) {
        myone = new Cat();
      }
      return myone;
    }
    

提交回复
热议问题