raii

RAII tutorial for C++ [closed]

我的未来我决定 提交于 2019-11-27 09:41:06
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . I'd like to learn how to use RAII in c++. I think I know what it is, but have no idea how to implement it in my programs. A quick google search did not show any nice tutorials. Does any one have any nice links to teach me RAII? 回答1: There's nothing to it (that is, I don't think you need a full tutorial). RAII

Local variable scope question

人走茶凉 提交于 2019-11-27 07:44:14
问题 Why is the following code prints "xxY"? Shouldn't local variables live in the scope of whole function? Can I use such behavior or this will be changed in future C++ standard? I thought that according to C++ Standard 3.3.2 " A name declared in a block is local to that block. Its potential scope begins at its point of declaration and ends at the end of its declarative region. " #include <iostream> using namespace std; class MyClass { public: MyClass( int ) { cout << "x" << endl; }; ~MyClass() {

RAII in Python - automatic destruction when leaving a scope

陌路散爱 提交于 2019-11-27 07:15:38
I've been trying to find RAII in Python. Resource Allocation Is Initialization is a pattern in C++ whereby an object is initialized as it is created. If it fails, then it throws an exception. In this way, the programmer knows that the object will never be left in a half-constructed state. Python can do this much. But RAII also works with the scoping rules of C++ to ensure the prompt destruction of the object. As soon as the variable pops off the stack it is destroyed. This may happen in Python, but only if there are no external or circular references. More importantly, a name for an object

How to initialize an object using async-await pattern

余生长醉 提交于 2019-11-27 05:49:37
问题 I'm trying to follow RAII pattern in my service classes, meaning that when an object is constructed, it is fully initialized. However, I'm facing difficulties with asynchronous APIs. The structure of class in question looks like following class ServiceProvider : IServiceProvider // Is only used through this interface { public int ImportantValue { get; set; } public event EventHandler ImportantValueUpdated; public ServiceProvider(IDependency1 dep1, IDependency2 dep2) { // IDependency1 provide

C# - Are objects immediately destroyed when going out of scope?

此生再无相见时 提交于 2019-11-27 02:47:50
问题 Can I trust that an object is destroyed and its destructor is called immediately when it goes out of scope in C#? I figure it should since many common coding practices (e.g. transaction objects) rely on this behaviour, but I'm not very used to working with garbage collection and have little insight to how such languages usually behave. Thanks. 回答1: Nope, .Net and hence C# relies on a garbage collection memory management. So destructors (which in .Net is called finalizers) are not called until

C/C++ macro/template blackmagic to generate unique name

这一生的挚爱 提交于 2019-11-27 02:46:14
Macros are fine. Templates are fine. Pretty much whatever it works is fine. The example is OpenGL; but the technique is C++ specific and relies on no knowledge of OpenGL. Precise problem: I want an expression E; where I do not have to specify a unique name; such that a constructor is called where E is defined, and a destructor is called where the block E is in ends. For example, consider: class GlTranslate { GLTranslate(float x, float y, float z); { glPushMatrix(); glTranslatef(x, y, z); } ~GlTranslate() { glPopMatrix(); } }; Manual solution: { GlTranslate foo(1.0, 0.0, 0.0); // I had to give

C++ read the whole file in buffer [duplicate]

筅森魡賤 提交于 2019-11-27 01:30:54
问题 This question already has answers here : How do I read an entire file into a std::string in C++? (12 answers) Closed 6 years ago . What is a good approach to read the whole file content in a buffer for C++? While in plain C I could use fopen(), fseek(), fread() function combination and read the whole file to a buffer, is it still a good idea to use the same for C++? If yes, then how could I use RAII approach while opening, allocating memory for buffer, reading and reading file content to

One-liner for RAII on non pointer?

喜你入骨 提交于 2019-11-27 00:57:40
问题 Related topic std::unique_ptr, deleters and the Win32 API To use a Win32 Handle as a RAII, I can use the following line std::unique_ptr<std::remove_pointer<HANDLE>::type, decltype(&CloseHandle)> m_mutex(CreateMutex(NULL, FALSE, NULL), &::CloseHandle); For me this is a clean one-liner and does exactly what I want. When it comes to SOCKET, it won't compile with this same line since SOCKET cannot be nullptr. What I need to do to make it work is the following : struct SocketDeleter { typedef

Does Java support RAII/deterministic destruction?

自作多情 提交于 2019-11-27 00:35:08
问题 It's been at least 5 years since I worked with Java, and back then, any time you wanted to allocate an object that needed cleaning up (e.g. sockets, DB handles), you had to remember to add a finally block and call the cleanup method in there. By contrast, in C++ (or other languages where object lifetimes are deterministic, e.g. Perl), the class implementor would define a destructor function that performs the cleanup whenever an object of that class goes out of scope. The advantage of this

Is there a proper 'ownership-in-a-package' for 'handles' available?

孤人 提交于 2019-11-26 22:26:57
问题 Handles have proper semantics other than pointers. So for me an example like this (extracted from the Rule of Zero): class module { public: explicit module(std::wstring const& name) : handle { ::LoadLibrary(name.c_str()), &::FreeLibrary } {} // other module related functions go here private: using module_handle = std::unique_ptr<void, decltype(&::FreeLibrary)>; module_handle handle; }; using unique_ptr as an 'ownership-in-a-package' for handles is a bad example. First, it makes use of