Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8707893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:58:40+00:00 2026-06-13T03:58:40+00:00

Every object-oriented language (or language which supports OOP), be it C++, Java, Python, PHP

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:58:41+00:00Added an answer on June 13, 2026 at 3:58 am

    Part 1

    /* memcpy example */
    #include <stdio.h>
    #include <string.h>
    
    struct {
      char name[40];
      int age;
    } person, person_copy;
    
    int main ()
    {
      char myname[] = "Pierre de Fermat";
    
      /* using memcpy to copy string: */
      memcpy ( person.name, myname, strlen(myname)+1 );
      person.age = 46;
    
      /* using memcpy to copy structure: */
      memcpy ( &person_copy, &person, sizeof(person) );
    
      printf ("person_copy: %s, %d \n", person_copy.name, person_copy.age );
    
      return 0;
    }
    

    taken from http://www.cplusplus.com/reference/clibrary/cstring/memcpy/

    Part 2

    1

    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

    function MyCloneRecursive(object A) : object
    {
        pointer Cpy;
        allocate memory to Cpy;
        memcpy(&Cpy,&A,sizeof(A))
        //now copy all its child elements
        //assuming there is a way to do a foreach that
        //for object A there are A.B,A.C and inside A.B there is D
        //and childname={"B","C"} and inside of B childname={"D"}
        for each childname in object A 
        {
            eval("Cpy." + childname + "=MyCloneRecursive(A." + childname + ")");
        }
    }
    
    //note this is really bad programming 
    //clone function is better written in the runtime
    //(or its a part of the intepreter not a includable code)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my project every class object has its own thread with infinite cycle (while(1))
This figure again shows that every object has a prototype. Constructor function Foo also
In Java you can wait on every object, so this kind of scenario is
I know that in OOP you want every object (from a class) to be
In Javascript, every object has a prototype. Any prototype is an object, therefore any
I have a collection companies. Every object in this collection has lastcall-param. It's a
In object-oriented paradigm, a virtual function or virtual method is a function or method
From my basic understanding in Object Oriented coding, PHP in my case, you want
I am new to taking an object oriented approach with php...actually I have a
So I'm trying to adopt good object oriented programming techniques with PHP. Most (read

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.