clone method of Object

grauzone none at example.net
Wed Apr 15 06:28:34 PDT 2009


Qian Xu wrote:
> Hi All,
> 
> is there any (easy) way to clone an object or any other classes?
> 
> 
> --Qian

Simple answer: No.

Complicated answer: Yes, but you have to write it yourself.

Here's a nice starting point. You can use tupleof to get all members of 
a class. Note that this doesn't deal with superclasses, and members of 
superclasses are not copied:

T clone(T)(T old) {
	auto newobject = new T();
	foreach (int i, _; old.tupleof) {
		newobject.tupleof[i] = old.tupleof[i];
	}
	return newobject;
}

cloned = clone(yourobject);



More information about the Digitalmars-d-learn mailing list