Class default constructor-Why to use the reference and when to explicitly give

Source: Internet
Author: User

First, the previous paragraph is simple.Code:

# Include <iostream>

Using namespace STD;

Class Test
{
Public:
Test ()
{
Cout <"default constructor" <Endl;
}

~ Test ()
{
Cout <"destructor" <Endl;
}

Void fun1 (Test & T)
{
Cout <"fun1 called" <Endl;
}

Void fun2 (test T)
{
Cout <"fun2 called" <Endl;
}
};

Int main ()
{
Test T1;

Test T2 (T1 );
T2.fun2 (T2 );
T1.fun1 (T2 );

Return 0;
}

Execution result:
Default constructor
Fun2 called
Destructor
Fun1 called
Destructor
Destructor
Analysis of the above execution results, we can see that Program It explicitly creates two test class objects, but the default constructor executes only once, but the Destructor executes three times. The constructor can have multiple versions, but the Destructor has only one version: Is there a default version, or you can provide it yourself. Therefore, the program creates three test class objects. The execution result only calls one constructor because of the existence of the default copy constructor. What is the form of the default copy constructor? When was it called? What should I pay attention to during use? -- To be continued

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.