Re:A brief introduction to C++'s model for type- and resourc...
by vvanders on Friday January 15, 2016 @01:00PM
(#10910896)
Sorry, I should have clarified that the init function is usually static so you can invoke with it returning an object + possible error code if you never want an object in an invalid state.
FWIW some quick googling shows that having a constructor acquire a resource is not a requirement(https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Resource_A...). The important part about RAII is that the automatic releasing of resources when out of scope(through a no-throw destructor).
Re:A brief introduction to C++'s model for type- and resourc...
by Manishearth on Friday January 15, 2016 @02:32PM
(#10911521)
You can solve this problem the Rust way too: don't use a constructor. Create a static `new` method that returns the object to you, with optional error flags.
This is still RAII, you're just not using a constructor to acquire. Rust doesn't have constructors and is still RAII.
That allows you to construct the object in an invalid state. The whole point of RAII is to avoid that. The acronym literally means not having a separate init() function.