3 ms·
>Consider what happens when initialisation of an object can fail. Constructors have no return values, so failure can be reported only by throwing an exception.
by hardware2win 4y ago
>Consider what happens when initialisation of an object can fail. Constructors have no return values, so failure can be reported only by throwing an exception. However, I've decided not to use exceptions. So we have to go for something like this:
class foo
{
public:
foo ();
int init ();
...
};
>When you create an instance of the class, constructor is called (which cannot fail) and then you explicitly call init function (which can fail).
Hmm? Use Builder and if you can then also Result<T>
- kevincox 4y agoThe best option here is a private constructor with public static "constructor functions" which can return something like Result<T>.