www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - making template interfaces work with inheritance

I'm having trouble making a template interface work with inheritance.  It works
fine for a single class, but once I inherit the superclass the compiler doesn't
seem to know which ICloneable(T) -- ICloneable!(Test1) or ICloneable!(Test2) is
required.

Any suggestions?  I'm trying to specify a generic ICloneable interface that a
whole heirarchy of objects will have to implement.

See the sample below.

Errors are:
test.d(19): class test.Test2 interface function ICloneable.Clone isn't
implemented
test.d(19): class test.Test2 interface function ICloneable.Clone isn't
implemented


Thanks,

Tom Johnson



module test;

interface ICloneable(T)
{
T Clone();
}

class Test1 : ICloneable!(Test1) {

void SomeMethod();

Test1 Clone() {
Test1 copyed=new Test1;
clobber(copyed,this);
return copyed;
}
}

class Test2 : Test1, ICloneable!(Test2) {

void AnotherMethod() {
super.SomeMethod();
}

Test2 Clone() {
Test2 copyed=new Test2;
clobber(copyed,this);
return copyed;
}
}

/* 
Code from Digital Mars Newsgroup
Title: Re: How to copy an object?
Author: "Ben Hinkle" <ben.hinkle gmail.com>
Date: Sat, 20 Aug 2005 21:32:12 -0400
Used by Clone methods.
*/
void clobber(Object dest, Object source) {
ClassInfo ci = source.classinfo;
if (ci !is dest.classinfo)
throw new Exception("Cannot clobber subclasses or superclasses");
void* s = source;
void* d = dest;
size_t start = Object.classinfo.init.length;
d[start .. ci.init.length] = s[start .. ci.init.length];
}
Feb 25 2006