Re: super.clone() puzzlement



On Thu, 20 Nov 2008, Derek Fountain wrote:

Generally, a copy constructor is more useful and less prone to mistakes.

public class MyClass {
public MyClass( MyClass mine ) {
//... make new class from old one...
}
}

I have a class like this:

class MyClass {
Object value;

// Implementation...
}

I want a copy of the MyClass instance where value references a deep copy of the value Object. That is, I want a copy of the MyClass instance where, when I change the object referenced by its value, a copy I've made of the MyClass instance keeps its own value object intact.

Am I right in thinking that given that I don't know what sort of Object value actually is, I've no way of reliably making a copy of it? Either with clone or a copy constructor or any other technique?

I guess I could change it to this:

class MyClass {
Cloneable value;
}

which would at least ensure the value was cloneable. But since I don't want to restrict what the user can store in value, I'm going to have to forego giving them a copy facility aren't I?

Probably.

Do the values have to be arbitrary objects, so could be a string, date, list, etc?

You might be able to do it if you define a sort of ValueCopier interface, and then find a convenient way to plug it in. Something like:

interface ValueCopier<T> {
public T copy(T obj) ;
}

class MyClass<T> {
private T value ;
private ValueCopier<T> copier ;
public MyClass(T value, ValueCopier<T> copier) {
this.value = value ;
this.copier = copier ;
}
public MyClass (MyClass<T> other) {
this.value = other.copier.copy(other.value) ;
this.copier = other.copier ;
}
}

You'd have to write a copier for each kind of thing you wanted to use as a value.

You might be able to use a static, global registry of copiers, organised as a map by class, to avoid having to fiddle around with copiers on a per-instance basis. That's a bit of a smelly solution, though.

tom

--
Also, a 'dark future where there is only war!' ... have you seen the
news lately? -- applez
.



Relevant Pages

  • Re: Is it good programming to set instance in class without using C-tor
    ... I have a class definition called MyClass see below. ... I create an instance of this class MyClass ... the question of what arguments should go on the constructor is ... your client has to test before they try to use an object instance. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Object creation overhead
    ... > class MyClass ... This is, of course, over and above the memory required for the ... > 'MyClass' objects themselves. ... > The 'proxy' class approach won't see a reduction in the number of ...
    (comp.lang.java)
  • Re: Changed class
    ... public int value1=0; ... private int oldvalue1=0; ... MyClass nn = new MyClass ... Class MyClass {public int value1=0; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: define and call methods in ruby
    ... def method_to_call ... It's the object (the instance of Myclass) that can execute the method. ... Intro to Ruby on Rails January 12-15 Fort Lauderdale, ...
    (comp.lang.ruby)
  • Re: super.clone() puzzlement
    ... public class MyClass { ... public MyClass(MyClass mine) { ... That is, I want a copy of the MyClass instance where, when I change the object referenced by its value, a copy I've made of the MyClass instance keeps its own value object intact. ...
    (comp.lang.java.programmer)