Know what functions C ++ silently and CILS
Today, I continue to learn about one of these terms, which gives me a better understanding of some of the internal operating mechanisms of C ++, I think C ++ is really well-designed in this respect. It is a very good language and C ++ has always been a popular language. Let's talk less. Each class has one or more constructors, one destructor, and one copy assignment operator. These control basic operations, such as generating a new object and ensuring that it is initialized, getting rid of the old object, ensuring that it is properly cleaned, and assigning new values to the object. So when you write an empty class, when you use the compiler to process the Code, it is not actually an empty class. The compiler will quietly generate
The default constructor, an destructor, a copy constructor, and a copy assignment operator, all of which are inline.Example code
A custom class: Class empty {};
It is actually equivalent to the following code:
Class empty {public: Empty () {}// default constructor ~ Empty () {}// destructor empty (const empty & RHs) {}// copy constructor empty & operator = (const empty & RHs) {}// copy assignment operator };
The concepts of these four terms are as follows:Default constructor: If you do not provide any constructor, the system provides a constructor that does not contain any parameters or function code;Destructor:In contrast to constructors, when an object is out of its scope (for example, the function of the object has been called), the system automatically executes the destructor. Destructor are often used to clean up the aftermath (for example, a piece of memory space is opened up with new when an object is created, and should be released with Delete in the Destructor before exiting ).Copy constructor:There is only one form parameter, and this form parameter is a reference to this type of object (commonly used const modifier). Such a constructor becomes a constructor.(Defined by C ++ pirmer ).Often referred to as X (const X &), which is also called automatically by compilation.Copy assignment operator:A value assignment operator automatically merged When will the copy constructor be called?
In the following three cases, a copy constructor of the class is called:
1) use an instantiated object of this class to instantiate another object of this class;
2) use the object Value passing method of this class as a function parameter;
3) the return value of a function is an object of this class. The following code is used as an example:
// Five. cpp: defines the entry point of the console application. // # Include "stdafx. H "# include <iostream> using namespace STD; Class myclass {public: myclass (): A (1), B (1) {} myclass (INT M, int N) {A = m; B = N;} myclass (myclass & X) {A = x. a; B = x. b; cout <"copy constructor is called. "<Endl;} friend int somefun1 (myclass X); friend myclass somefun2 (int A, int B); Private: int A; int B ;}; int fun1 (myclass X) {return X. A + X. b;} myclass fun2 (int A, int B) {myclass Ca (a, B); Return CA;} int _ tmain (INT argc, _ tchar * argv []) {myclass A; // call the declared constructor myclass C (10, 10); myclass D (c); // case 1) int anint = fun1 (C ); // case 2 myclass E = fun2 (11, 11); // case 3 return 0 ;}
When must I explicitly declare a copy constructor?
The function of copying constructor is to instantiate another object of the class with an instantiated class object. The following code does not explicitly declare a constructor. The Compiler automatically generates a default implicit copy constructor for the class baseclass:
// Five. cpp: defines the entry point of the console application. // # Include "stdafx. H "# include <iostream> using namespace STD; Class baseclass {PRIVATE: int A; public: baseclass (int B) {A = B;} void setvalue (int) {This-> A = A;} void show () {cout <Endl ;}}; int _ tmain (INT argc, _ tchar * argv []) {baseclass base (100); baseclass Dev = base; // call the default implicit copy constructor baseclass BC (Dev); // call the default implicit copy constructor Dev. show (); // The output should be 100 Dev. setvalue (90); Dev. show (); // The output should be 90 base. show (); // The output is 100 BC. show (); // The output should be 100 return 0 ;}
Note that the Destructor generated by the compiler is non-virtual (see Article 7 ), unless the base class of this class itself declares a virtual destructor (in this case, the virtual attribute of this function.When will the copy assignment operator not be automatically called?As for the copy constructor and the copy assignment operator, the version created by the compiler simply copies each non-static member variable of the source object to the target object. However, in some cases, the compiler rejects the generation of the copy assignment operator function. For example, there are referenced members and const members. Can a reference be changed? If yes, it violates the C ++ principle: references cannot be modified to object. Therefore, you must define your own copy
Assignment operator. For example, the following code:
// Five. cpp: defines the entry point of the console application. // # Include "stdafx. H "# include <iostream> using namespace STD; class person {public: Person (string & A, const Int & B): Name (A), ID (B) {} PRIVATE: const int ID; string & name;}; int _ tmain (INT argc, _ tchar * argv []) {person p1 (string ("chu "), 1); person P2 (string ("Jun"), 2); P1 = P2; // error c2582: 'operator = 'function is unavailable in 'person 'system ("pause"); Return 0 ;}
In another case, the compiler does not generate the copy assignment function, that is, the base type declares the copy assignment as private, and the derived type cannot be used by the compiler. Because the derived type cannot call the copy assignment function of the base type (does not have access permissions ).
#include "stdafx.h"#include <iostream>using namespace std;class BaseClass {private: BaseClass& operator=(const BaseClass& rhs) { }};class derived : public BaseClass { };int _tmain(int argc, _TCHAR* argv[]) { BaseClass px1, px2; px1 = px2; ///// 'BaseClass::operator =' : cannot access private member declared in class 'BaseClass' system("pause"); return 0;}
After the above explanation, we must have made the four default constructor, copy constructor, copy assignment operator, and destructor concepts clear enough ~ So
Remember
The compiler can secretly create default constructor, copy constructor, copy assignment operator, and destructor for the class.