Is it possible to prevent stack allocation of an object and only allow it to be instantiated with 'new'?

后端 未结 6 1027
孤城傲影
孤城傲影 2020-11-30 03:44

Is it possible to prevent stack allocation of an object and only allow it to be instiated with \'new\' on the heap?

6条回答
  •  悲&欢浪女
    2020-11-30 04:03

    You could make the constructor private, then provide a public static factory method to create the objects.

提交回复
热议问题