making template interfaces work with inheritance
Tom Johnson
Tom_member at pathlink.com
Sat Feb 25 21:38:15 PST 2006
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 at 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];
}
More information about the Digitalmars-d-learn
mailing list