Constructors and destructors

Source: Internet
Author: User

1. Constructor function

After we have defined a class, we instantiate it, the value is random, and then we assign it to ourselves in the write function, which feels superfluous,

C + + provides the process of initializing at instantiation, which is called a constructor function;

Constructors are special public member functions that have the following characteristics:

(1), the constructor and the class name are the same;

(2), the constructor does not write the return value;

(3), when the object is instantiated, automatically must be called once ;

(4), can be overloaded (life and inequality, so understanding);

(5), constructors can be defined within a class, or can be defined outside the class (through scope qualifiers::)

(6), C + + will automatically give the default constructor, if you write, you will not provide the default constructor;

Class Test{public:test (int d = 0): data (d) {}

This is a default constructor that, when present, can no longer have a parameterless constructor (otherwise initialized to a random value or a default value of 0);


2.3 Functions of a constructor:

(1), constructs the object, automatically calls the constructor;

(2), initialize the object;

(3), type conversion

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/84/67/wKiom1ePiTaAuCLpAACH_37wURA325.png-wh_500x0-wm_3 -wmp_4-s_3072058079.png "title=" Qq20160720222206.png "alt=" Wkiom1epitaauclpaach_37wura325.png-wh_50 "/>


Analysis: T1 is the type of test, 100 is plastic, and how can different types be assigned?

:c C + + are strongly typed languages, if the type is not the same, from a certain point of view is not allowed to assign value ;

int a;double d = 12.34;a = D;

Type is different, it is not possible to assign a value directly, the use of intermediate temporary variable to assign value;


Now see t = 100;

100 must find the middle Bridge test type, in order to give the test type T assignment, which is the corresponding member to assign value;

Just have a constructor, and there is a parameter, through the constructor, the shaping 100 into the test type, at this time instantiate an object, just converted to the same type, you can assign a value.


At this point there is also a problem: the keyword explicit display, the role: prevent the constructor is implicitly called ;

This keyword can only be used before the constructor;

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/84/67/wKiom1ePj_fiY6uLAAC7W9ZTkEs198.png-wh_500x0-wm_3 -wmp_4-s_4214818854.png "title=" Qq20160720225116.png "alt=" Wkiom1epj_fiy6ulaac7w9ztkes198.png-wh_50 "/>

At this time implicit assignment, cannot call the display constructor, so error;


650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/84/67/wKioL1ePkRfyaw0pAACa68-F61I779.png-wh_500x0-wm_3 -wmp_4-s_631793548.png "title=" Qq20160720225547.png "alt=" Wkiol1epkrfyaw0paaca68-f61i779.png-wh_50 "/>

Test T = (test) 100; Coercion type conversion is the display call, the constructor of explicit adornment can be called!


Now look at the following question: Can the test type assign a value to the int type?

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/84/67/wKioL1ePksvS1B2GAAC0zNxCKLI274.png-wh_500x0-wm_3 -wmp_4-s_521534895.png "title=" Qq20160720230313.png "alt=" Wkiol1epksvs1b2gaac0znxckli274.png-wh_50 "/>

The two sides of the type are inconsistent, and the function that transforms the same type Test->int type is not found inside the class (i.e. no intermediate bridge is found);

Must be used for operator overloading (Add method), the following is called type conversion, ———— > can not have a return value;

Add a method within the class:

Public:operator Int () {return data; }

Convert the test type to int, which can be achieved with this method, to find the bridge, and the type is uniformly assignable;

Principle: The use of the middle of the nameless Variable space (to act as a bridge);

The results are as follows:

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/84/67/wKiom1ePlPGCTDvhAACu5sMWEEo409.png-wh_500x0-wm_3 -wmp_4-s_2131356729.png "title=" Qq20160720231156.png "alt=" Wkiom1eplpgctdvhaacu5smweeo409.png-wh_50 "/>


3. destructor

~test () {}

Destructors are the space used to release objects, and the same is true for any function death, so that the destruction of any object can only be done in the same way,

destructors have no overloads, no parameters, no return, only one ; (Death is equal).

Characteristics:

(1), the destructor is the same as the class name, but preceded by ' ~ ', such as: ~test () {}

(2), when the object is released, the system automatically calls the destructor (which can be the default).


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.