Background introduction
The allocation of resources is unavoidable in the process of running a C + + program-especially in the game! Resources can be in many, from textures, sounds, shader code to handles, and strings that can be called resources. The management of resources is an important round in the project, if not done well, then memory leaks, heavy memory crashes. RAII is an important programming idea for resource management in C + + projects.
First, a little C + +
The essential thing in C + + is class, and each class is indispensable to constructors and destructors. The former is used for a series of operations when an object is constructed, which is used for functions that are executed when the object is being refactored.
It is worth mentioning that in C + +, if a class is declared in the stack space, the class will automatically call the destructor after the function has finished executing from the stack space. However, if the display is declared in heap space (using the new method or the malloc method), you need to display the call to delete or the free function for destruction.
The above is to read this blog needs to understand the C + + knowledge, it should not be difficult ...
General concept
C + + has a lot of wonderful names, such as Yacc,raii, which is a long and proud tradition 2333333.
RAII is a typical example of "resource acquisition and initialization" (Resource aquisition is initialization), rather than some people think of "initialization as a resource acquisition" (Initialization is Resource Acquisition). BTW, if you want to be funny, blame the end, or not reach the effect.
RAII's technology is simple, using the concept of C + + object life cycle to control the resources of the program. Its technical rationale is 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 + + comes with an object management facility that can manage resources.
...
The above is the C + + learning RAII programming ideas content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!