Bill Roper (billroper) wrote,
Bill Roper

How I Learned To Stop Worrying And Love clone()

So I'm still working on porting my code from C++ to Java. I'm over the 40,000 line mark now, which is good, but there is still a lot of stuff left to port. I've been working through the Var class for several days and am nearing the 6000 line mark there. (It's a large and important class with many methods.) And as I work down through it, I run into many other classes that aren't completed where I either go "I'll fix that later" or "I should go fix that now".

So I needed to copy an object that held an array of other complex objects that held still more complex objects along with a metric ton of ordinary data. And I was about to swear and write yet another copy constructor (because I am a recalcitrant C++ programmer apparently) when I suddenly realized that I was about to do way too much work.

If I implement the Cloneable interface and the clone() method on these objects, then I get a free bitwise copy of everything that they contain. So for that object with a metric ton of ordinary data, I just call super.clone() and I have now copied the metric ton of ordinary data and only need to fix up and clone the complex objects that I want to have new copies of, since my intent is to do a deep copy when calling clone().

So I ran around and implemented all of the necessary clone functions for half a dozen classes.

And the code base got 47 lines longer. Instead of a whole lot more lines of mechanical copying which would have pumped up the line count, but not actually done any more work.


OK. I guess I'm not yet too old to learn some new tricks. :)
Tags: c++, computers, java, musings, work

  • Planning. Planning Would Be Good

    Gretchen and I have a concert slot on Saturday for the Festival of the Living Rooms. We are now trying to figure out what we should put in it. Well,…

  • Bits and Pieces

    I am rather surprised to find that the Cardinals are suddenly in sole possession of the second wild-card berth in the NL. I am keeping my fingers…

  • Yet Another Pulled Pork Sandwich

    Gretchen and I went to City Barbeque for lunch yesterday and I decided to try the pulled pork sandwich there for the first time. This particular…

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded