Interface question

David Medlock noone at
Thu Aug 31 04:41:41 PDT 2006

Charles D Hixson wrote:
> xs0 wrote:
>>> 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.
>> Hmm.. why exactly do you need a common constructor type? When you 
>> create an object, you must know its exact type (and constructors) 
>> anyway, so how does it matter?
>> xs0
> I think maybe you're right.  If so, then I probably can't do what I want 
> to do in D.  I'd been thinking that if I just worked ahead I'd be able 
> to solve the problem of "somehow" using the thing that I'd created, if I 
> could only create it. And I could refer to it from the ancestral 
> reference.  But I hadn't been able to figure out the details, so I was 
> just trying to work my way forwards.  But maybe I can't do it at all.
> (Well, this isn't literally true, of course.  I could always define 
> everything in terms of a Vector of bytes.  But in so doing I give up 
> most of what makes Computer Languages different from assembler.)
> I guess that means that it's back to Python.  At least I can drop into 
> Pyrex when I hit places that are performance bottlenecks.  Or even into 
> C.  (I may truly dislike C with it's wild use of pointers, but it *is* 
> possible.  And it's about the same speed as D. [I don't really believe 
> that shootout that says that D is faster.  How *can* it be?])

(Ignore this if I am misreading you - David)

I don't quite 'get' what you are trying to accomplish(not technically 
but in a design sense).

Unless you are attempting to add compiled classes at runtime, I would be 
surprised if D could not accomplish what you are doing.

To create an object which has a known constructor, use a template 
function/object....but there is a better way.

Have your objects created with no argument constructors then configured 
using a common method.  I have a text format(XML lite) I use in which 
each node is a 'constructor' for an object.  Then the children of the 
node are passed to the config() method of the object.  In this way I can 
have many similar objects which are combined in different ways.

Is this close to what you are trying?

More information about the Digitalmars-d-learn mailing list