|
|
|
|
|
by mikepurvis
3286 days ago
|
|
I don't think this is the proposal. The proposal is that the object contains a genuine constructor that only does the bare-bones "safe" stuff, and then it has a separate non-static method that does the might-fail initialization. So: class Foo
{
public:
Foo();
bool initialize(); // returns success
...
};
...
Foo foo;
if ( !foo->initialize() ) { // handle error }
This also means you can break up your initialization so that you drive the risky pieces from outside the object, rather than monolithically from within.This has a further benefit for testing, since you can use your major objects without fully initializing the entire world that they depend on. |
|
It's also a technique that is widely used. See for example the cocos2d-x game library.
The benefit of such a technique is that you can then make the constructor private, making it impossible to create an object and not also call the initialize() method.