top | item 44674179

(no title)

henry700 | 7 months ago

The natural solution for this is a private constructor with public static factory methods, so that the user can only obtain an instance (or the error result) by calling the factory methods. Constructors need to be constrained to return an instance of the class, otherwise they would just be normal methods.

Convention in OOP languages is (un?)fortunately to just throw an exception though.

discuss

order

Conscat|7 months ago

In languages with generic types such as C++, you generally need free factory functions rather than static member functions so that type deduction can work.