![]() ![]() Possibility 2: Throwing a exception also takes care of the life cycle of the objects in the current scope, thus doing proper cleanup. ![]() Possibility 1: Return essentially leaves the current function scope, so it knows about the end of the life cycle of os thus calling its destructor and doing proper cleanup by closing and flushing the file to disk. ![]() Int possibility = /* either 1, 2, 3 or 4 */ What happens if you don't get to the destructor to flush and close the file? Who knows! But possibly it won't write all the data it was supposed to write into the file.įor instance consider this code #include For instance the std::ofstream class open the file during the constructor, then the user performs output operations on it, and finally at the end of its life cycle, usually determined by its scope, the destructor is called that essentially closes the file and flushes any written content into the disk. RAII and Stack UnwindingĬ++ makes use of a idiom called RAII, which in simple terms means objects should perform initialization in the constructor and cleanup in the destructor. There are several ways, but first you need to understand why object cleanup is important, and hence the reason std::exit is marginalized among C++ programmers. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |