Home >Backend Development >PHP Tutorial >Detailed explanation of copy constructor and assignment operator overloading in PHP_PHP Tutorial

Detailed explanation of copy constructor and assignment operator overloading in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:13:421053browse

This article introduces in detail the overloading of copy constructors and assignment operators in PHP. Students who need to know more can refer to it.

Assignment and copying of objects: Assignment: overloaded through the "=" operator

User a(b);
The code is as follows
 代码如下 复制代码

User a(10),b;

b = a;

复制:调用复制构造函数

User b;

User a(b);

或者

User a = b;//相当于User a(b);

Copy code

User a(10),b;

b = a;

 代码如下 复制代码

User getUser()

User temp; 

return temp;

}

int main()

User user = getUser();//调用getUser();

}

Copy: Call copy constructor

User b;
or

User a = b;//Equivalent to User a(b); The difference between assignment and assignment is that assignment is to assign a value to an existing object (the assigned object has been implemented and defined), while copying is to create a new object from scratch and make it consistent with the existing object. same. Shallow copy and deep copy: If there is a pointer member in the object, only the address of the pointer member will be copied to the newly created object. Therefore, the pointer members in both objects point to the same memory. Area, there will be repeated release problems when releasing. You need to manually define a copy constructor. In the constructor, new memory will be allocated for pointer variables. The pointer members of different objects point to different memory areas. Three situations when the copy constructor is used: 1. You need to create a new object and initialize it with another object of the same type. 2. When the parameter of the function is an object of the class, you need to create an actual parameter when calling the function. Copy, copy a formal parameter according to the actual parameter, the system is implemented by calling the copy constructor 3. The return value of the function is the object of the class: At the end of the function call, the object in the function needs to be copied to a temporary object and passed to The calling point of this function.
The code is as follows Copy code
User getUser()
{
User temp; return temp; } int main() { User user = getUser();//Call getUser(); } When the getUser() function call ends, the life cycle of the object temp created in getUser ends (will be destroyed), so instead of bringing temp back to main, the copy constructor of the User class is called when the return statement is executed. Press temp to copy a new object and assign it to user. http://www.bkjia.com/PHPjc/629095.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629095.htmlTechArticleThis article introduces in detail the copy constructor and assignment operator overloading in PHP. Students who need to know more can Please refer to it. Assignment and copying of objects: Assignment: through...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn