C ++ constructor and Destructor from scratch (2): initialization list (const and reference member), copying Constructor

Source: Internet
Author: User

I. constructor initialization list

We recommend that you initialize the constructor in the initialization list.
The execution of constructor is divided into two phases.

Initialization Section

Common Calculation Section

(1) object members and their initialization

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Include <iostream>
Using namespace STD;

Class Object
{
Public:
Object (INT num): num _ (Num)
{
Cout <"object" <num _ <"..." <Endl;
}
~ Object ()
{
Cout <"~ Object "<num _ <"... "<Endl;
}
PRIVATE:
Int num _;
};

Class container
{
Public:
Container (INT obj1 = 0, int obj2 = 0): obj2 _ (obj2), obj1 _ (obj1)
{
Cout <"container..." <Endl;
}
~ Container ()
{
Cout <"~ Container... "<Endl;
}

PRIVATE:
Object obj1 _;
Object obj2 _;
};

Int main (void)
{
Container C (10, 20 );
Return 0;
}

From the output, we can see the following points: first, the object members must be constructed before the object is constructed; second, the order of object member construction is related to the order of definition, and has nothing to do with the order of initialization list; third, the construction order is the opposite to the Destructor order. Fourth, if the class corresponding to the object member does not have a default constructor, the object member can only be initialized in the initialization list. If the class is inherited and the base class does not have a default constructor, the base class constructor must be called in the initialization list of the derived class constructor.


(2) const member and reference member Initialization

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Include <iostream>
Using namespace STD;

// Const member initialization can only be performed in the const initialization list
// The initialization of referenced members can only be performed in the constructor initialization list.
// Initialize an object member (the class corresponding to the object member does not have the default constructor). It can only be performed in the constructor initialization list.
Class Object
{
Public:
Enum e_type
{
Type_a = 100,
Type_ B = 200
};
Public:
Object (INT num = 0): num _ (Num), knum _ (Num), refnum _ (Num _)
{
// Knum_= 100;
// Refnum _ = num _;
Cout <"object" <num _ <"..." <Endl;
}
~ Object ()
{
Cout <"~ Object "<num _ <"... "<Endl;
}

Void displayknum ()
{
Cout <"knum =" <knum _ <Endl;
}
PRIVATE:
Int num _;
Const int knum _;
Int & refnum _;
};

Int main (void)
{
Object obj1 (10 );
Object obj2 (20 );
Obj1.displayknum ();
Obj2.displayknum ();

Cout <obj1.type _ A <Endl;
Cout <obj2.type _ A <Endl;
Cout <object: type_a <Endl;

Return 0;
}


Because both the const variable and reference have to be initialized during definition, the const member and reference member must be initialized in the initialization list. In addition, you can use the defined enumerated type to obtain constants in the class scope.


Ii. copy constructors

(1) copy constructors

Function: uses an existing object to initialize a new object of the same type.
Declaration: there is only one parameter and the parameter is a reference to the Class Object test: Test (const Test & other)
;
If the copy constructor is not defined in the class, the system automatically generates a default copy constructor. As a public member of the class, the system also performs simple member replication.

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Ifndef _ test_h _
# DEFINE _ test_h _

Class Test
{
Public:
// If the class does not provide any constructor, the system will provide us with
// Default constructor
Test ();
Explicit test (INT num );
Test (const Test & other );
Void display ();

Test & operator = (const Test & other );

~ Test ();
PRIVATE:
Int num _;
};
# Endif/_ test_h _

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Include "test. H"
# Include <iostream>
Using namespace STD;

// A constructor without parameters is called the default constructor.
Test: Test (): num _ (0)
{
// Num _ = 0;
Cout <"Initializing default" <Endl;
}

Test: Test (INT num): num _ (Num)
{
// Num _ = num;
Cout <"Initializing" <num _ <Endl;
}

Test: Test (const Test & other): num _ (other. Num _)
{
// Num _ = Other. Num _;
Cout <"Initializing with other" <num _ <Endl;
}

Test ::~ Test ()
{
Cout <"Destroy" <num _ <Endl;
}

Void test: Display ()
{
Cout <"num =" <num _ <Endl;
}

Test & test: Operator = (const Test & other)
{
Cout <"test: Operator =" <Endl;
If (this = & other)
Return * this;

Num _ = Other. Num _;
Return * this;
}

C ++ code
1
2
3
4
5
6
7
8
9
10
# Include "test. H"

Int main (void)
{
Test T (10 );
// Test T2 (t); // call the copy constructor
Test t2 = T; // equivalent to test T2 (t );

Return 0;
}


That is, the copy constructor is called. The two destroy parameters are T and t2.


(2) Copying constructor calls

When the function parameter is a class object, it is used to call the function and participate in the combination of real parameters. Create a local object in the memory and copy the real parameters to the new object. The copy constructor is also called.


When the return value of a function is a class object, it is used to return the caller after the function is executed. The reason is to create a temporary object and then return the caller. Why not use the partial object to be returned directly? Because a local object disappears when it leaves the function that creates it, and it cannot survive after the function is called. In this case, the compilation system creates an unknown temporary object in the expression that calls the function. The life cycle of the temporary object is only in the expression at the function call. The so-called return object actually calls the copy constructor to merge the value of this object into a temporary object. If a variable is returned, the process is similar, but the constructor is not called.

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Include "test. H"
# Include <iostream>
Using namespace STD;

Void testfun (const test T1)
{

}

Void testfun2 (const Test & T1)
{

}

Test testfun3 (const Test & T1)
{
Return T1;
}

Const Test & testfun4 (const Test & T1)
{
// Return const_cast <Test &> (T1 );
Return T1;
}

Int main (void)
{
Test T (10 );
Testfun (t );

Cout <"..." <Endl;

Return 0;
}


That is, the copy constructor is called when the parameter is passed. When the function returns, T 1 of testfun has reached the survival time, T1 is destroyed before the split line output, and T is destroy.


Replace testfun (t); with testfun2 (t );


If the parameter is referenced, the copy constructor is not called.


Replace testfun (t); with T = testfun3 (t );


When the function returns, it calls the copy constructor, then calls the value assignment operator, releases the temporary object, and finally releases T. If t is not used for receiving, the temporary object will be released immediately.


Replace testfun (t); with test t2 = testfun3 (t );


If the function returns a call to the copy constructor, but does not call the copy constructor again, and the temporary object is not released, it can be understood that the temporary object is changed to T2.


Replace testfun (t); with Test & t2 = testfun3 (t );


When the function returns, the copy constructor is called. Because T2 references a temporary object, it is not immediately released.


Replace testfun (t); with test t2 = testfun4 (t );


The copy constructor is not called for parameter passing and response. during initialization of T2, the copy constructor is called.


Replace testfun (t); with const Test & t2 = testfun4 (t );


No constructor is called for parameter passing and response. If T2 is a reference, no copy constructor is called.


Refer:

C ++ primer version 4
Valid tive C ++ 3rd
C ++ programming specifications

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.