Interface/abstract constructors
nrgyzer
nrgyzer at gmail.com
Mon May 16 13:12:05 PDT 2011
== Auszug aus Steven Schveighoffer (schveiguy at yahoo.com)'s Artikel
> On Mon, 16 May 2011 15:32:43 -0400, useo <unknown at unknown.com>
wrote:
> > Hey guys,
> >
> > is there any chance to create an abstract constructor like:
> >
> > abstract class ABC {
> >
> > abstract this();
> >
> > }
> >
> > DMD always says "...this non-virtual functions cannot be
abstract" -
> > when I use an interface like:
> >
> > interface ABC {
> >
> > this();
> >
> > }
> >
> > I get a similar error: "...constructors, destructors, postblits,
> > invariants, unittests, new and delete functions are not allowed in
> > interface ABC"
> >
> > Is there any solution or is it possible to create such
inheritances
> > in DMD?
> I think what you are trying to do is say, "if a class implements
interface
> ABC, it must have a default constructor". Such a requirement is
faulty.
> The point of an interface is to able to pass a portion of a class'
> functionality to a function during runtime. However, the instance
must
> *already exist*. It makes no sense to posit requirements on the
> constructor.
> What you want is a compile-time requirement using a template
constraint.
> You may think "damn, but I don't want to make my function a
template", I'd
> say see previous point ;)
> -Steve
Okay, thanks... perhaps someone know a better solution: I have one
big file which contains some other files (let's say: blocks). Each
block has it's own signature... by reading the big file, I read the
signature of each block. Based on the signature, I read block A,
block B or another Block. To do that, I want call the block-specific
constructor which reads the next bytes.
!Semicode:
...
ABC[] blocks;
...
while (!eof(bigfile)) {
read(signature);
if (signature==A) blocks ~= new A(bigfile);
else if (signature==B) blocks ~= new B(bigfile);
...
}
...
More information about the Digitalmars-d-learn
mailing list