Can I typedef a member?

I am using a std::pair to represent some coordinates:

typedef std::pair<double,double> cart2D; // 2D cartesian coordinates

and it would be really nice if I could access the coordinates as "x" and "y" instead of "first" and "second", like this:

1
2
3
cart2D mycoord;
mycoord.x = 1.0;
mycoord.y = -1.0;


Is there an easy way to do this, preferably with a typedef? I don't think it's worth it creating a class, but if there were a simple way of achieving this it would improve readability somewhat. Thanks.
Not with typedef. Typedef creates an alias for a type, not a variable. You need to make a new class.
You can wrap an std::pair in a class you create yourself, having two references to the pair members. Or create your own pair class, maybe with operator std::pair().
There's also inheritance, but I remember reading that standard library classes should not be derived from.
That's a shame. Thanks though :)
Topic archived. No new replies allowed.