How to handle incorrect values in a constructor?

后端 未结 11 1359
野性不改
野性不改 2020-12-17 08:44

Please note that this is asking a question about constructors, not about classes which handle time.

Suppose I have a class like this:



        
11条回答
  •  独厮守ぢ
    2020-12-17 09:32

    There is one more possible way. I am not saying this is in any way preferred, only adding it for completeness:

    Create a factory function that creates an instance of your class on the heap, and returns a null pointer if the creation fails.

    This is not really appropriate with valuetype-like objects as dates, but there might be useful applications.

提交回复
热议问题