constructor of constructors and destructor of destructors (II.)

Source: Internet
Author: User

(1) Conversion constructors

The definition of a transformation constructor: a transformation constructor is a constructor that converts a normal built-in type into a class type, which has only one parameter. A constructor that contains only one parameter, which can be used as two constructors, one for initializing objects and one for converting constructors

1 //test.h2 #ifndef Test_h3 #defineTest_h4 classtest{5     intm_i;6  Public:7Test (inti =0);//conversion constructors, also common constructors8~Test ();9 Ten }; One #endif //Test_h A  -  -  the //Test.cpp -#include"Test.h" -#include <iostream> - usingstd::cout; + usingStd::endl; -  +  ATest::test (inti): m_i (i) { atcout <<"Test (int i):"<<"m_i="<<m_i<<Endl; - } -test::~Test () { -cout <<"~test ()"<<Endl; -  - } in  -  to //Demo.cpp +  -#include <iostream> the#include"Test.h" * intMain () { $Test T ( A);//the normal constructor is called to initialize the TPanax Notoginsengt = the;//at this point, the conversion constructor is called to convert 15 to a class object, creating a temporary object -      the     return 0; +  A}

In the t=15 code above, a temporary object is generated, so when is the temporary object released?

#ifndef Test_h#defineTest_hclasstest{intm_i; Public: Test (inti =0); ~Test ();};#endif //Test_h#include"Test.h"#include<iostream>usingstd::cout;usingStd::endl; Test::test (inti): m_i (i) {cout<<"Test (int i):"<<"m_i="<<m_i<<Endl;} Test::~Test () {cout<<"~test ()"<<Endl;} #include<iostream>#include"Test.h"intMain () {Test T ( A);// call constructor, execute test (int i): m_i=12 t= the;// Generate temporary object, call transformation constructor Test (int i): m_i=15,after assigning the temporary object to T, the destructor is called and the temporary object with the M_i value of 15 is released.
Test t2;// calls the default constructor, test (int): m_i=0;  The destructor is then called to release the T and T2 objects
return 0 ; }

The constructor of a class has only one parameter that is very dangerous, because the compiler can use this constructor to implicitly convert the type of the parameter to the class type. So sometimes we add a keyword explicit in front of the constructor so that the normal built-in type cannot be implicitly converted to a class object.

The difference between the assignment operator and the initialization:

1 //Add the following function to the class to overload the assignment operation2 Consttest& Test::operator=(Consttest&VT) {3M_i =vt.m_i;4     return* This;5 }6 7 8 9 Ten#include <iostream> One#include"Test.h" A intMain () { -Test T ( A);//only constructors are called at this time -t = the;//The constructor constructor is called to generate a temporary object, and when the temporary object assigns a value to T, the overloaded function that calls the = sign the      -Test t2=9;//only the constructor is called and there is no overloaded function that calls the = sign -t2 = t;//call = Sign overload, at which point the = sign is an assignment operator.  -     return 0; +  -}

According to the Set breakpoints experiment, trace operator overload function and constructor can see, t=15 This sentence is to call the constructor, and then call the operator overload function, the description is a temporary variable, and then assign the temporary variable to t,test t2=9 this sentence only called the constructor, So this sentence belongs to initialization, t2=t this sentence just called the operator overload function, so the = number in this sentence is an assignment.

constructor of constructors and destructor of destructors (II.)

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.