void pointer syntax

H. S. Teoh hsteoh at quickfur.ath.cx
Tue May 15 19:56:49 PDT 2012


On Wed, May 16, 2012 at 04:01:23AM +0200, Stephen Jones wrote:
[...]
> Using Object gives exactly the same problem as the Object super
> class does not have vcount variable. Casting is not a solution
> because the reason for throwing different sorts of widgets into a
> single array was so I did not have to track what each type of object
> was; not tracking what each object in the array is I have no means
> of knowing what to cast each Widget in the array to.

If you did not know what type each object was, how do you know they have
a vcount variable?

If you _do_ know that they have a vcount variable, that means you know
what type to downcast to. If there is more than one subclass with a
vcount variable, that's an indication that you want to factor it into a
common base class that gives a unified access to it, in which case you'd
downcast to the common base class.


> If some one knows void pointer syntax that would be helpful. As I
> understand it there is a type called size_t that takes the address
> of as a a value. Can I make an array of these and simply initialize
> each with &button1, &cursor, etc? Also, how is size_t freed?

Void pointer does not help you at all. You can't access the vcount
variable through a void pointer unless you know it's a particular type
that has such a variable -- you'd have to cast the void pointer to that
type first. And in that case, you might as well be downcasting instead
because it's type-safe. Blindly casting a void pointer into a particular
type because you _think_ that's what it is, is a recipe for disaster
when later on your list changes and the void pointer no longer points to
what you think it points to.


T

-- 
If lightning were to ever strike an orchestra, it'd always hit the conductor first.


More information about the Digitalmars-d-learn mailing list