Interface question
Charles D Hixson
charleshixsn at earthlink.net
Tue Aug 29 10:31:45 PDT 2006
BCS wrote:
> Charles D Hixson wrote:
>> How should I write some code that would have an effect similar to what:
>> interface P
>> { this(ulong ndx);
>> ulong fset();
>> }
>>
>> would have if it were a legal construct?
>>
>> The "constructor" needs to return an instance of the class that
>> implements it.
>
> It strikes me as odd that you would want to do this. (I would have to
> see the usage though before I would say you shouldn't.) As I understand
> it the intention of Interfaces is that the underlying implementation is
> /totally/ hidden. If there is some action that the class needs to do at
> instancing time, then that should be apparent to the person coding the
> class and done by them.
>
> OTOH, if the "this" function is doing something like registering the
> class with some sort of global catalog, you might have need for
> something like that. However I would make the registration action
> associated with the catalog and not the Objects
>
> /******
> Objects implementing P must register with the Catalog
> ******/
> interface P
> { this(ulong);
> ulong fset();
> }
>
> class Catalog
> {
> void Add(P);
> void Remove(P);
> }
>
> Catalog cat;
>
> class PO : P
> {
> ulong fset(){...}
>
>
> this(ulong f)
> {
> ...
>
> cat.Add(cast(P)this);
> }
>
>
> ~this()
> {
> cat.Remove(cast(P)this);
> }
>
> // OR
>
> /****************
> ALWAYS registrar this class with the Catalog
> ****************/
> }
>
That's essentially what I wanted to do (with variations...it's
to disk, and the methods are save and restore)...but the
interface is invalid!
I guess what I'm going to need to do is derive everything from
a common ancestral class...and that means that the derived
classes CAN'T descend from anything else, *sigh*. The common
ancestor will need to be a totally abstract class, so I
thought an interface would be a better choice, but it looks
like there's no way to make it work...because they've all got
to implement a common constructor type, and the compiler has
to KNOW that they do so.
More information about the Digitalmars-d-learn
mailing list