Arrays of Unions of Classes?

DLearner via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Feb 6 10:55:29 PST 2015


I'm just wondering how I would go about reserving a section of
the heap so I can have linear access to classes of different
types. Storage space--not too worried about wasting; each class I
want to store only has a few int sized variables each and I'm not
going to cry over a little padding.

Because classes are reference types, does that mean a union or an
array would only hold a reference to that class? Is there a way
for them to be squished up next to each other in a way that'd
make iterating over them efficent? I could do this with structs
(at least I think I can in D) except they're not polymorphic and
structs don't remember their type internally like classes do when
inside unions (I just tested this, I assume it's a feature not a
bug?).

I have a lot of elements in an array that I need to pass over
constantly so if I can not break them up and not have to make up
pretend internal polymorphism with switch statements inside
functions then that'd both make my code look less like spaghetti
and leave room for me to bog down my program with other crap. How
do I do this in D?


More information about the Digitalmars-d-learn mailing list