For example:
private:
std::vector<std::string> _content;
public:
Document (const std::string& path) :_content(FileIO().read(path)) { }
Is it OK to call a function ( read() )
directly with an "temporary" Object ( FileIO() )
?
I'm asking because it seems very convenient, but I have never seen code like this before. Just want to know if this is "valid" C++ code, rather than some weird anti pattern or performance "no no".
It's fine. path
is well-defined, and the anonymous temporary will be valid during initialisation of the member _content
.
You should note that if an exception is thrown by FileIO().read(path)
then ~Document()
will not be called.