void pointer syntax

Stephen Jones siwenjo at gmail.com
Mon May 14 11:40:30 PDT 2012


I want an array of different classes of objects. I tried to
subsume the differences by extending the classes under a single
interface/abstract class/super class (3 different approaches) all
to no avail as I could not access the public variables of the
instantiated classes while storing them in an array defined by
the interface/super class.

interface Widget{
void draw();
}

class Button : Widget{
public int vertCount;

void draw(){}
}

class Cursor : Widget{
public int vertCount;

void draw(){}
}

//called from
Widget[] widgets;
widgets~=new Button();
widgets~=new Cursor();

foreach(Widget w; widgets){
writeln(w.vertCount);
}
//Error: no property 'vertCount' for type 'Widget.Widget'


A solution that should work is to dump extending the classes and
make an array of void pointers that point, some to Button objects
and some to Cursors but I do not know the syntax for doing this,
or even if it is possible to have an array of pointers pointing
at different classes of objects. I should think it should be
possible given that, on a 32 bit machine, pointers are all 32 bit
ints so it is basically an array of ints that is being created,
only those ints contain the address of a bunch of different types
of objects.


More information about the Digitalmars-d-learn mailing list