RAII programming ideas for C++ learning

黄舟
Release: 2017-02-06 14:07:27
Original
1945 people have browsed it

Background Introduction

In the process of running a C++ program, resource allocation is inevitable-especially in games! There can be many resources, ranging from textures, sounds, shader codes to handles and strings. These things can be called resources. Resource management is a very important step in the project. If it is not done well, it can range from memory leaks to memory crashes. RAII is an important programming idea used for resource management in C++ projects.


Let’s talk about C++ first

The indispensable thing in C++ is the class, and the indispensable thing for each class is the constructor and destructor . The former is used for a series of operations performed when the object is constructed, and the latter is used for the functions performed when the object is destructed.


It is worth mentioning that in C++, if a class is declared in the stack space, the class will automatically pop up from the stack space after the function is executed. Call the destructor. However, if it is explicitly declared in the heap space (using the new method or malloc method), you need to explicitly call the delete or free function to destruct it.


The above is the C++ knowledge required to understand this blog, it shouldn’t be difficult...


Overall concept

C++ has many weird names, such as yacc, raii, etc. This can be regarded as a long and proud tradition2333333.


RAII is a very typical example. It means "Resource Aquisition Is Initialization" (Resource Aquisition Is Initialization), not "Initialization Is Initialization" as some people think. Initialization is resource acquisition. BTW, if you want to be weird, just blame it, otherwise the effect will not be achieved.


RAII's technology is very simple, using the concept of C++ object life cycle to control program resources. Its technical principle is very simple. If you want to track an important resource, create an object and associate the life cycle of the resource with the life cycle of the object. In this way, C++'s own object management facilities can manage resources.


...


The above is the content of RAII programming ideas for C++ learning. Please pay attention to more related content. PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template