Detailed description of copying constructors and value assignment operators in php _ PHP Tutorial

Source: Internet
Author: User
Detailed description of copying constructors and value assignment operators in php. This article describes in detail how to copy constructors and assign value operators in php. if you need to know more, refer to the example below. Assignment and replication of objects: Assignment: This article describes in detail the copy constructor and value assignment operator overloading in php. for details, refer to the following.

Object assignment and copying: value assignment: reload using the "=" operator

The code is as follows:

User a (10), B;

B =;

Copy: Call the copy constructor.

User B;

User a (B );


User a = B; // equivalent to User a (B );

The difference between a value assignment and a value assignment is to assign a value to an existing object (an object with a value defined), while copying creates a new object from nothing, and make it the same as an existing object.

Shallow replication and deep replication: if a pointer member exists in an object, only the address of the pointer member is copied to the newly created object. therefore, the pointer members of both objects point to the same memory area, and the issue of repeated release may occur during release. You need to manually define the replication constructor. in the constructor, a new memory will be allocated for the pointer variable, which indicates that pointer members of different objects direct to different memory regions.

When copying Constructors: 1. you need to create a new object and initialize it with another similar object. 2. when the function parameter is a class object, when calling a function, you need to create a copy of the real parameter and copy a form parameter according to the real parameter. The system is implemented by calling the copy constructor. 3. the return value of the function is the class object: at the end of a function call, you need to copy a temporary object from the function and pass it to the function call place.

The code is as follows:

User getUser ()


User temp;

Return temp;


Int main ()


User user = getUser (); // call getUser ();


When the call to the getUser () function ends, the life cycle of the object temp created in getUser is terminated (to be destroyed). Therefore, when the return statement is executed, call the copy constructor of the User class, copy a new object according to temp, and assign it to the user.

Bytes. Object assignment and copying: value assignment: through...

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: 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.