Constructors and destructors

Source: Internet
Author: User

First, the constructor function and the destructor

Reference Link: http://www.cnblogs.com/mr-wid/archive/2013/02/19/2917911.html

1. Constructors
constructors are primarily used to perform some initialization of object properties when an object is created, and when an object is created, the object automatically calls its constructor. In general, constructors have the following three functions:

    • Establishes an identifier for the created object;
    • Open up memory space for object data members;
    • Completes initialization of the object data member.

2. Destructors

The destructor, in contrast, is called automatically when an object is revoked, for some cleanup work when the member is revoked, such as the previously mentioned manual release of the requested memory space using new or malloc. Destructors have the following characteristics:
The destructor function name is the same as the class name, and it is closely preceded by the name with the tilde ~ and the constructor, for example: ~point ();
Constructors have no return type and cannot specify parameters, so destructors can only have one and cannot be overloaded;
Destructors are called automatically when an object is revoked, and unlike constructors, destructors can be explicitly called to free memory that is dynamically requested in the object.

Constructors and destructors

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.