Making containers that "go both ways"

janderson askme at me.com
Fri Jan 25 23:06:41 PST 2008


Bill Baxter wrote:
 > Hey,
 > Just wanted to throw this idea out for comment.
 > I find I can't really decide whether a container class should be a 
struct or a class.  Struct feels right if I want minimal overhead and 
something that works more like a built-in datatype.  But class feels 
right for situations where I'm less concerned about performance


Personally I'd make it a class.  Yes its going to go on the heap however 
the resizeable array part or whatever the container contains, will 
probably go on the heap anyway.

, and
 > also offers the additional ability to extend the basic functionality 
via subclassing.

I think you should avoid thinking about subclassing like this.  Using 
mixins or components is a much better idea then subclassing. Subclassing 
should normally be used for polymorphism.

 >
 > So something I did (a while back now...) was to create some 
containers in OpenMesh/D that use mixins for all their guts, and provide 
both struct and class wrappers for them.
 >
 > It seems to me like that might be the "right" way to do containers in 
D. Somewhat unfortunately, because it's more complicated than just 
writing one or the other.
 >
 > Thoughts?
 >
 > --bb


This may help:
http://www.artima.com/intv/goldilocks3.html



More information about the Digitalmars-d mailing list