[Cocos2d-x Study Notes 02] object Creation Method Comparison

Source: Internet
Author: User
1. the way to create an object instance is compared to the way to get an object instance in the cocos2d-x and the General C ++ programming method is somewhat different, the following is a piece of code, it compares the different compiling methods of object creation, initialization, and destruction:
Generally, the code for creating C ++ objects is as follows:
Object *obj = new Object(arg1, arg2);delete obj;
Generally, C ++ objects are initialized in their constructors, and new objects need to be released by themselves.
The object creation code in the cocos2d-x:
Object *obj = Object::create();
The cocos2d-x returns the pointer to the object by calling the static function create () of the class, and the new object is released by the cocos2d-x's memory management mechanism, so there is no need to worry about memory release issues. The create () function generally has three operations: 1) create an object. The constructor does not contain complex logic and is only responsible for initializing member variables. 2) Call the initialization functions of the object, such as Init () and initwithxx (), and return the bool value to identify whether the initialization is successful. 3) The autorelease () function that calls the object inherited from the base class ref is handed over to the cocos2d-x for memory release.
Object *Object::create(){    Object *obj = new Object;    if (obj && obj->init())    {         obj->autorelease();    }    else    {         delete obj;         obj = nullptr;    }    return obj;}
The macro create_func (_ type _) is provided in the cocos2d-x to complete the above create () function definition, but it can only create a create () function without parameters, if you need to input parameters in the CREATE () function, you can write them yourself.

2. Advantages and DisadvantagesGenerally, C ++ creates objects in a way that intuitively conforms to the general C ++ programming habits. The disadvantage is that you need to release resources yourself (you can use the sharing pointer STD :: shared_ptr to avoid errors to the maximum extent ).
The cocos2d-x creates an object in a way that initializes member variables in the constructor, initializes the object in the initialization function, and creates () the static function in the class () the first two steps are completed and the memory is managed by its internal memory management mechanism. It may be a bit winding, but it is still good to use it once used to it. The biggest advantage of this method is that you don't have to worry about the release of new objects, so it is more suitable for the case that the class inherits from ref, so that you can get the benefits of Automatic Memory Management brought about by the cocos2d-x. 3. Summary when writing your own class, if the class functions are much considered to inherit from the class ref, to get automatic memory management, and use the cocos2d-x to create the style of the object. If a class has a single function, such as vec2 and size, consider using the General C ++ style to create an object instance.

[Cocos2d-x Study Notes 02] object Creation Method Comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.