C ++ replication Constructor

Source: Internet
Author: User

Copy constructor, direct initialization, copy initialization, assignment, temporary object

  Several questions should be clarified about the replication constructor: when to call the replication constructor, what are the features of the replication constructor, and why should we define our own replication constructor.

  1. Copy constructor:When you do not define your own replication constructor, the system will generate a default replication constructor. When an object is passed by value, a temporary object of the form parameter is created, and the copy constructor is called to copy the value of the temporary object to the real parameter.

  2By default, the function of copying constructor is as follows:Copy the values of non-static members of an object to another object one by one. Note that the value of the member is copied. This replication method is also called shortest replication. Because static members belong to the entire class and do not belong to an object, static members are not affected when calling the copy constructor.

  3. When to generate a temporary object: Scenario 1:When passing objects by value, note that objects are transmitted by value. Passing by value means that a copy of the original object will be created,

  Scenario 2:When the function returns an object.

  Case 3:When initializing another object with an object, it is the replication initialization. The statements hyong x = y and hyong x = hyong (y) Here y are hyong objects. Will call the replication constructor, but it is possible to create a temporary object or directly initialize the object using the replication constructor without creating a temporary object, depending on the compiler.

4. The temporary object is created by the copy constructor. When the temporary object disappears, the corresponding destructor is called. That is to say, once a temporary object is created, the Destructor will be called one more time.

  5. When to use the replication constructor:Pass objects by value. The function returns objects. When one object is used to initialize another object, that is, the array elements are initialized Based on the element initialization list. In both cases, the copy constructor is called. Remember, the copy constructor can only be used for initialization. It cannot be used for value assignment. Instead of calling the copy constructor when assigning values, it uses the value assignment operator.

  6. Initialize directly:Direct initialization places the initialization formula in parentheses. For Class types, direct initialization always calls constructors that match the real parameters for initialization,

  7. Replication initialization and replication constructor:During replication initialization, The = equals symbol is used for initialization. Replication initialization also creates a new object and its initial value comes from another existing object. Replication initialization always calls the replication constructor for initialization, during replication initialization, you first create a temporary object using the specified constructor, and then copy each non-static member of the temporary object to the newly created object in sequence using the replication constructor. The copy constructor executes Member-by-member initialization. Note that an existing object is used to create another new object. Unlike creating a new object directly using a constructor, another object is not used for initialization using a constructor. For example, if there is a class of hyong, the statement hyong m () calls the constructor for direct initialization, while the statement hyong n = m uses the existing object m to initialize a new object n, replication initialization.

  8. Understand the difference between value assignment and replication initialization (Key points):A value assignment is performed between two existing objects, that is, an existing object is used to change the value of another existing object. The value assignment operator calls the value assignment operator to operate the object. The value assignment operator will be explained in the operator overload. For example, if there is a class hybriong, there is a statement hybriong x (1); Hyong y () Then X = y; this is the value assignment, because the object X and Y are existing objects, the statement Hyong x = Y is the replication initialization, and an existing object y is used to create a new object X, so it is the replication initialization.

9. Copying initialization and assignment are performed between two objects, but direct Initialization is not.

  10. Note:Using the copy constructor does not necessarily create a temporary object, just like the statement Hyong x = Hyong (Y). Where Y is a Hyong object, it is possible that no temporary object is created, depending on the compiler. If a temporary object is created, the Destructor will be called once when the temporary object dies. if the object is not called, The Destructor will not be called if the object is initialized directly using the copy constructor.

  11. Copy the constructor form:Hyong (const Hyong & OBJ); it accepts a constant reference pointing to a class object as a parameter. It is required to define Const. Because the copy constructor only copies objects, it is not necessary to change the value of the passed objects. It can save time to declare it as a reference, if it is passed by value, it will generate a copy of the object, which will waste resources, but the reference will not.

  12. Why do you need to define your own replication constructor:If a class only contains members of the class type and built-in type, you do not need to display the definition of the replication constructor. If the class contains pointers or other types of resources, you must redefine the replication constructor. Because the class has pointer members, when initializing another object with an object, the pointers of both objects point to the same memory segment. If one of the objects is destroyed, at this time, the memory pointed to by the pointer in the object is also destroyed, but the other object does not know this situation, then there will be a problem. For example, the Hyong class contains a member pointer P. When Hyong x = y is declared, Y is also the object of the Hyong class. In this case, the pointer member P in object X and Y points to the same memory segment, if y is destroyed, but X is not destroyed, a problem may occur. In this case, the member pointer P of the object in Y has released the memory resource, the member pointer P in X does not know that the resource has been released, and a problem occurs. Because the Member pointers in object X and Y share the same memory segment, modifying the member pointer P in object y affects the Member pointers in object X. In all these cases, you need to redefine the value of the initialization member displayed by the replication constructor. This initialization method is also called Deep replication.

13. If the replication constructor is defined, the system calls the show replication constructor to initialize the object directly. If the definition replication constructor is not displayed, the default replication constructor is called to initialize the object directly.

  14. Note: 1.In VC ++, the statement hyong n = m does not generate a temporary object. However, if a replication constructor is defined, the system calls the show replication constructor to initialize object n directly, if no replication constructor is displayed, call the default replication constructor to initialize object n directly.

2. in VC ++, the statement hyong m1 = hyong (m) may generate temporary objects or do not generate temporary objects, if the replication constructor is defined, use the replication constructor to initialize the object m1 directly without generating a temporary object. If no definition replication constructor is displayed, the replication constructor creates a temporary object and initializes the object m1.

15. C ++ automatically provides the following member functions: default constructor, copy constructor, default destructor, value assignment operator, and address OPERATOR: this pointer, if you do not define these five functions, the system automatically creates one.

  16. Directly call the constructor in the class:You can directly call the constructor of a class in the class function, an independent function outside the class, that is, the main () function, for example, the main function can contain the statement n = A (4); here n is the object of Class A, and here it is to directly call the constructor of Class A to create A temporary object of Class, then, assign the value of the temporary object to object n of Class. Functions in a class are similar to constructor methods in a class called by functions outside the class. Note that statement n. A (4) is an incorrect statement and cannot be called by constructor in the class of the object.

  Example: use of the copy constructor

Class hyong

{Public: int a, B, c; hyong () {a = B = c = 0; cout <"gouchao" <"/n ";} hyong (int I) {a = B = c = I; cout <"gouchao2" <"/n ";}

~ Hyong () {cout <"xigou" <"/n";} hyong (const hyong & obj) {a = B = c = 9; cout <"fuzi" <"/n" ;}// copy constructor .};

Void H (Hyong K) {cout <"haoshu" <K. A <K. B <"/N" ;}// pass objects by value

Hyong F () {Hyong m3 (5); Return m3 ;}// returns the object.

// If the replication constructor is defined, the system calls the display replication constructor to initialize the object directly. If the definition replication constructor is not displayed, the default copy constructor is called to initialize the object directly.

Int main ()

{//The following are several replication initialization methods.

Hyong M (1 );

// Hyong n = m and Hyong M1 = Hyong (m) Whether to generate a temporary object depends on the Compiler

Hyong n = m; // in VC ++, this statement does not generate a temporary object. It calls the display-defined copy constructor to initialize the object.

Cout <m. A <m. B <"/N"; // output 99

Cout <n. A <n. B <"/N"; //, output 99. Call the replication constructor defined to initialize object N without generating a temporary object.

Hyong M1 = Hyong (m); // pay special attention to this statement, because this statement may generate a temporary object or not generate a temporary object, if a replication constructor is defined, the object M1 is initialized directly using the replication constructor, instead of generating a temporary object. If no definition replication constructor is displayed, the replication constructor generates a temporary object and initializes M1.

Cout <m1.a <m1. B <"/N"; // output 11. Call the display-defined replication constructor to initialize the object M1 without generating a temporary object. If no replication constructor is defined, a temporary object is generated and the Destructor is called when the temporary object is revoked.

Hyong m2 (m); cout <m2.a <m2. B <"/n"; // output 11, directly calling the copy constructor, so no temporary object is generated

Hyong * p = new hyong (m); cout <p-> a <p-> B <"/n"; // No temporary object is generated, directly call the copy constructor for initialization.

  //Example of passing and returning objects by value.

H (m); cout <"kkk" <"/n"; // pass object m by value, when function h is called, a temporary object is generated using the copy control function, and then the temporary object is copied to the real parameter. When the function is called, the temporary object is revoked, at this time, a destructor will be called, which is called only when the scope of the h function disappears. That is to say, the Destructor will be called only after the h function body is executed.

Hyong m4 = f (); cout <m4.a <m4. B <"/n"; // output 55. Use the returned object to initialize the m4 object. This statement does not generate a temporary object, the reason is unclear. It may be related to the statement copy initialization.

Hyong m5; m5 = f (); // This statement calls the function f. f returns an object and calls the copy constructor to generate a temporary object, the temporary object is used as a parameter of the default value assignment operator. Therefore, not only the copy constructor is called, but also the value assignment operator is called.

Cout <m5.a <m5. B <"/n ";

Hyong m6; m6 = m; // assign the m value to m6. Note that the copy constructor is not called or a temporary object is not generated, here m is treated as a parameter of the default value assignment operator .}

  For example, directly call the constructor in the class.

Class A {public: int a; A () {a = 0 ;}a (int I) {A = I ;}~ A () {cout <"xi" <"/n ";}

A f () {return A (3) ;}; // call the class constructor in the class, when this function is called by an object, it returns A temporary object constructed by constructor.

A g () {return A (5) ;}// method of the constructor of the class called by A function outside the class. Note that the function name is directly used here.

Int main ()

{A m (1); A n (2); n = m. f (); cout <n. a <"/n"; // Output 3: calls the f function in the class. The f function returns a temporary object using the constructor.

N = g (); cout <n. a <"/n"; // output 5, call function g outside the class

N = A (6); cout <n. a; // directly call the constructor in the main function to create a temporary object, and then assign the value of this temporary object to object n.

// N. A (7);} // error. The constructor cannot be called using class objects.

 

Go to: Baidu encyclopedia

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.