4 ms·
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
by henry700 1y 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.
- Conscat 1y agoIn languages with generic types such as C++, you generally need free factory functions rather than static member functions so that type deduction can work.
- deleted 1y ago[deleted]