Every object-oriented language (or language which supports OOP), be it C++, Java, Python, PHP has a clone function of its own, which can return a deep or shallow copy of an object. Can anybody tell me how to create my own clone function from scratch, for both deep and shallow copy? Obviously I can’t use any language-construct like serialize, not to mention clone itself! An example in PHP would be great, though any other language is fine as well, I need to know how to do it, that’s all.
Every object-oriented language (or language which supports OOP), be it C++, Java, Python, PHP
Share
Part 1
taken from http://www.cplusplus.com/reference/clibrary/cstring/memcpy/
Part 2
I’m assuming you need to copy all child elements of complicated objects(as in copying imaginary object A in figure 1,the code I’ve linked above can be used if you need only to copy the black color object but keeping others as references , the below pseudo code is for if you want to copy its all child items as well)
Above figures shows how the reality and how we think of an object