C + +: destructors

Source: Internet
Author: User

Features of the destructor:
1, the destructor is the same as the class name, but it must precede the wave number ~
2, the destructor does not return any value, when the destructor is defined, it cannot describe its type, or even the type of void can not
3. Destructors do not have parameters and therefore cannot be overloaded. A class can have multiple constructors, but only one destructor
4. When the object is revoked, the compilation system will automatically call the destructor

//Example 3.13 Conplex class containing destructors and constructors#include<iostream>#include<cmath>using namespacestd;classcomplex{ Public: Complex (DoubleR=0.0,DoubleI=0.0);//constructors that declare default parameters~complex ();//declaring destructors       DoubleAbscomplex ();//declaring the Abscomplex function Private:       DoubleReal; DoubleImag; }; Complex::complex (DoubleRDoubleI//Defining Constructors{Real=R; Imag=i; cout<<"Constructor called"<<Endl;} Complex::~complex ()//defining Destructors{cout<<"destructor called"<<Endl;} DoubleComplex::abscomplex ()//defining the Abscomplex function{       DoubleT; T= real*real+imag*Imag; returnsqrt (t);}intMain () {Complex A (1.1,2.2); cout<<"the absolute value of a complex number is:"<<a.abscomplex () <<Endl;return 0;}

Description
(1) Each class must have a destructor. If you do not explicitly define a destructor for a class, the compiled system
A default destructor is automatically generated. For example, the compilation system generates a default for class complex similar to the following form
destructor: Complex::~complex () {}
Similar to the default constructor automatically generated by the system, this automatically generated default destructor function body is also empty, but
It can complete the task of releasing the memory space occupied by the object.
For most classes, this default destructor will satisfy the requirements. However, if an object is revoked before
If the requirement completes other processing work, the destructor should be defined as shown, for example:

classstring_data{ Public: String_data (Char*s)//constructor Function{str=New Char[Strlen (s) +1];//dynamically allocating a storage space with the operator new for string strstrcpy (str,s); }       ~string_data ();// Destructors{Delete str; //releasing dynamically allocated storage space with the delete operator       }        voidGet_info (Char*); voidSent_info (Char*); Private:       Char*str; };

This is a common use of constructors and destructors to allocate storage space for strings in constructors with the new operator
, and finally frees the allocated storage space through the delete operator in the destructor.

(2) In addition to the end of the main function (or call the Exit Function), the object is revoked, the system will automatically call the destructor function
, the destructor is also called in the following cases:
A, if an object is defined in the function body, then when the function is called at the end, the object will be disposed, the destructor
function is called automatically.
b, if an object is created dynamically using the new operator, when the delete operator is used to release it, the delete
The destructor is called automatically.

#include <iostream>using namespacestd;classstring_data{ Public: String_data (Char*s)//constructor Function{str=New Char[Strlen (s) +1];//dynamically allocating a storage space with the operator new for string strstrcpy (str,s); }       ~string_data ()// Destructors{cout<<"destructor"<<Endl;    Delete str; //releasing dynamically allocated storage space with the delete operator       }        voidGet_info (Char*); Private:       Char*str; };voidString_data::get_info (Char*str1) {STR=str1; cout<<str<<Endl;}intMain () {String_data SD ("ABC"); Sd.get_info ("Xiayuanquan"); return 0;} 

C + +: destructors

Related Article

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.