how to access struct member using [] operator?

Nicholas Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Sep 24 23:36:25 PDT 2016


On Sunday, 25 September 2016 at 04:54:31 UTC, grampus wrote:
> Dear all
>
> For example, I have a struct
> struct point{int x;int y}
> point a;
>
> Is there an easy way to access x and y by using a["x"] and 
> a["y"]
>
> I guess I need to overload [], but can't figure out how.
>
> Someone can help? Thank you very much

The way you access the memers of a struct is through the dot 
syntax.

i.e.

a.x = a.y;

> Is there an easy way to access x and y by using a["x"] and 
> a["y"]

in this case "x" and "y" are strings so no.


More information about the Digitalmars-d-learn mailing list