C + + class construction, copy construction, destructor, etc.

Source: Internet
Author: User

1:

An empty class is no longer empty after the C + + compiler is processed, and the compiler automatically declares some member function for us if you write


Class a{};
After the compiler is processed, it is equivalent to:

Class A
{
Public
A (); Default constructor
A (const a&); Copy constructor
~a (); Destructors
a& operator= (const a& RHS);
A * operator& (); Fetch address operator

Const * operator& () const;
};

Basically, I answered the answer above, just a few two functions that take the address operator.

Is this answer right?

In fact, for such an empty class, it is completely unnecessary, and the compiler does not do so. The compiler's approach is:

Only if you need to use these functions and you do not show the declaration of these functions, the compiler will be intimate to automatically declare the corresponding function.

Like what
A;
The compiler generates constructors and destructors for Class A based on the example above.
When using
A B (b);
The compiler generates a copy constructor for Class A.
A C;
c = A;
Compiler-generated assignment operator functions
a &d = A;
The compiler generates the fetch address operator function.

This is understood by our analysis: for an empty class that is not instantiated, the compiler does not generate any functions for it, and when an empty class is instantiated, the compiler generates the corresponding function as needed. This theory is also suitable for non-empty classes (declaring variables only, not declaring functions).

2

Const member variable, static const, static member variable initialization for a class

Conclusion:

    • A static constant data member can be initialized within a class ( that is, initialized at the same time as declared within a class), or it can be initialized in an implementation file outside of a class, which cannot be initialized in a constructor or initialized in a constructor's initialization list;
    • Static and very data members can only be initialized in an implementation file outside the class, that is, in the constructor, and cannot be initialized in the initialization list of the constructor function;
    • A non-static constant data member cannot be initialized within a class, nor can it be initialized in a constructor, but only and must be initialized in the constructor's initialization list;
    • nonstatic non-static data members cannot be initialized within a class, can be initialized in constructors, or can be initialized in a constructor's initialization list;

Summarize the following table:

Type initialization mode

In -Class ( declaration )

Out -of-Class ( class implementation file )

In the constructor

Initialization list of constructors

Non-static, very-mass data members

N

N

Y

Y

Non-static constant data member

N

N

N

Y (MUST)

Static and very data members

N

Y (MUST)

N

N

Static constant data member

Y

Y

N

N

/** * <effective c++>, page 14* const data of class* platform:visual Studio 2005, win32* FILENAME:ITEM2.1.CPP*/#include<iostream>using namespacestd;&nbsp;classmytest{//(1) Error C2864: ' Mytest::maxnumber1 ': only static const integral data, can be initialized within a class /c8>//int MaxNumber1 = 5;&nbsp; //(2) Error C2864: ' Mytest::maxnumber2 ': only static const integral data, can be initialized within a class /c0>//const int MaxNumber2 = 5;&nbsp; //(3) Error C2864: ' Mytest::maxnumber3 ': only static const integral data, can be initialized within a class /c5>//static int MaxNumber3 = 5;&nbsp; //(4) OK    Static Const intMaxNumber4 =5; Static Const CharCconst4 ='B';&nbsp; //(5) Error C2864: ' MyTest::d const4 ': only static const integral data members can be initialized within a class //static const Double DCONST4 = 200.00;&nbsp; Public:    //(6) Error C2758: ' Mytest::maxnumber2 ': Must is initialized in constructor base/member initializer listMyTest () {cout<<"MyTest constructor!"<<Endl; cout<<"MaxNumber4 ="<<MaxNumber4<<Endl; cout<<"Cconst4 ="<<cconst4<<Endl; }};&nbsp;intMain () {MyTest obj; return 0;}

In the code comment (1), (2), (3) represents the step number.

From (1), (2), (3) , it can be seen that only the static const integral data member ( constant data member) can be initialized within the class . From (4), (5) can also be proved. where thechar type is equivalent to an integral type.

The results of the operation are as follows.

MyTest constructor!

MaxNumber = 5

Cconst1 = A

Cconst2 = B

Dconst1 = 100

/** * <effective c++>, page 14* const data of class* platform:visual Studio 2005, win32* filename:item2.2.cpp*/#include<iostream>using namespacestd;&nbsp;classmytest{intMaxNumber1; Const intMaxNumber2; Static intMaxNumber3;&nbsp; Static Const intMaxNumber4 =5; Static Const CharCconst4 ='B';&nbsp; Static Const intMaxNumber5;&nbsp; Public:    //(1) Error C2758: ' Mytest::maxnumber2 ': Must is initialized in constructor base/member initializer list//(4) Error C2438: ' MaxNumber3 ': Cannot initialize static class data via constructor//(7) Error C2438: ' MAXNUMBER5 ': Cannot initialize static class data via constructorMyTest (): MaxNumber1 (5), MaxNumber2 (5)//, MaxNumber5 (5)//, MaxNumber3 (5)    {        //(2) Error c2166:l-value specifies const object//MaxNumber2 = 5;&nbsp; //(3) Error lnk2001:unresolved external symbol "Private:static int mytest::maxnumber3" (email protected]@@0ha) //MaxNumber3 = 5;&nbsp; //(6) Error C3892: ' MAXNUMBER5 ': cannot assign to a variable it is const//MaxNumber5 = 5;&nbsp; cout<<"MyTest constructor!"<<Endl; cout<<"MaxNumber1 ="<<MaxNumber1<<Endl; cout<<"MaxNumber2 ="<<MaxNumber2<<Endl; cout<<"MaxNumber3 ="<<MaxNumber3<<Endl; cout<<"MaxNumber4 ="<<MaxNumber4<<Endl; cout<<"MaxNumber5 ="<<MaxNumber5<<Endl; cout<<"Cconst4 ="<<cconst4<<Endl; }};&nbsp;//(5) OKintMytest::maxnumber3 =5;&nbsp;//(8) OKConst intMytest::maxnumber5 =5;&nbsp;//(9) Error C2761: ' int mytest::maxnumber1 ': member function redeclaration not allowed//int mytest::maxnumber1 = 5;&nbsp;intMain () {MyTest obj;&nbsp; return 0;}

The results of the operation are as follows.

MyTest constructor!

MaxNumber1 = 5

MaxNumber2 = 5

MaxNumber3 = 5

MaxNumber4 = 5

MAXNUMBER5 = 5

Cconst4 = B

In the code comment (1), (2), (3) represents the step number.

From (1), (2) It can be seen that a non-static constant data member must be initialized in the initialization list of the constructor, or, if initialized in the constructor, an error c2166 that the constant object is read only , you cannot assign a value to it.

From (3), (4), (5), static and very data members can only be initialized outside of the class ( implementation file of the class ) .

From (6), (7), (8), Static constant data members can also be initialized outside of the class ( the implementation file of the class ) .

C + + class construction, copy construction, destructor, etc.

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.