raii

Does C++ support 'finally' blocks? (And what's this 'RAII' I keep hearing about?)

China☆狼群 提交于 2019-11-26 01:29:24
问题 Does C++ support \'finally\' blocks? What is the RAII idiom? What is the difference between C++\'s RAII idiom and C#\'s \'using\' statement? 回答1: No, C++ does not support 'finally' blocks. The reason is that C++ instead supports RAII: "Resource Acquisition Is Initialization" -- a poor name † for a really useful concept. The idea is that an object's destructor is responsible for freeing resources. When the object has automatic storage duration, the object's destructor will be called when the

Is it abusive to use IDisposable and “using” as a means for getting “scoped behavior” for exception safety?

无人久伴 提交于 2019-11-26 01:14:32
问题 Something I often used back in C++ was letting a class A handle a state entry and exit condition for another class B , via the A constructor and destructor, to make sure that if something in that scope threw an exception, then B would have a known state when the scope was exited. This isn\'t pure RAII as far as the acronym goes, but it\'s an established pattern nevertheless. In C#, I often want to do class FrobbleManager { ... private void FiddleTheFrobble() { this.Frobble.Unlock(); Foo(); //

Understanding the meaning of the term and the concept - RAII (Resource Acquisition is Initialization)

让人想犯罪 __ 提交于 2019-11-26 00:39:41
问题 Could you C++ developers please give us a good description of what RAII is, why it is important, and whether or not it might have any relevance to other languages? I do know a little bit. I believe it stands for \"Resource Acquisition is Initialization\". However, that name doesn\'t jive with my (possibly incorrect) understanding of what RAII is: I get the impression that RAII is a way of initializing objects on the stack such that, when those variables go out of scope, the destructors will

Why is it wrong to use std::auto_ptr<> with standard containers?

夙愿已清 提交于 2019-11-25 23:33:43
问题 Why is it wrong to use std::auto_ptr<> with standard containers? 回答1: The C++ Standard says that an STL element must be "copy-constructible" and "assignable." In other words, an element must be able to be assigned or copied and the two elements are logically independent. std::auto_ptr does not fulfill this requirement. Take for example this code: class X { }; std::vector<std::auto_ptr<X> > vecX; vecX.push_back(new X); std::auto_ptr<X> pX = vecX[0]; // vecX[0] is assigned NULL. To overcome

RAII and smart pointers in C++

旧时模样 提交于 2019-11-25 22:24:42
问题 In practice with C++, what is RAII, what are smart pointers, how are these implemented in a program and what are the benefits of using RAII with smart pointers? 回答1: A simple (and perhaps overused) example of RAII is a File class. Without RAII, the code might look something like this: File file("/path/to/file"); // Do stuff with file file.close(); In other words, we must make sure that we close the file once we've finished with it. This has two drawbacks - firstly, wherever we use File, we

throwing exceptions out of a destructor

佐手、 提交于 2019-11-25 21:57:00
问题 Most people say never throw an exception out of a destructor - doing so results in undefined behavior. Stroustrup makes the point that \"the vector destructor explicitly invokes the destructor for every element. This implies that if an element destructor throws, the vector destruction fails... There is really no good way to protect against exceptions thrown from destructors, so the library makes no guarantees if an element destructor throws\" (from Appendix E3.2) . This article seems to say

What is meant by Resource Acquisition is Initialization (RAII)?

纵饮孤独 提交于 2019-11-25 21:39:46
问题 What is meant by Resource Acquisition is Initialization (RAII)? 回答1: It's a really terrible name for an incredibly powerful concept, and perhaps one of the number 1 things that C++ developers miss when they switch to other languages. There has been a bit of a movement to try to rename this concept as Scope-Bound Resource Management , though it doesn't seem to have caught on just yet. When we say 'Resource' we don't just mean memory - it could be file handles, network sockets, database handles