Clone a Class

So - is there an easy way to clone an instance of a class?

I have a Sphere() class that’s expensive to instantiate - I’d like to make one, then copy it, and modify the copy.

But if I say “s=Sphere()” then “s2=s” - they’re both references to the same object, modifying one modifies the other.

If theres no easy way, I can work around it by breaking out the expensive part (vertex calculations). But it would be cleaner if there was an easy way to copy-by-value.

The usual way to do this would be to implement a clone() or copy() method in your objects that does a deep copy.

So s2 = s:clone()

Internally, clone() would figure out how to copy all the data into a new Sphere object and return that object.

That’s what I eventually did - was just trying to figure out if there was an easier way.