Alias for a member variable

1
2
3
4
5
class LongitudeLatitude : public std::pair<double,double>
{
};


Now that class has 2 members at least. Namely first and second.

Say loc is an object of that class. Say I want to refer loc.first and loc.Longitude instead. How would I do so?
You can't. The only way to do it would be to use a macro, but that would be an abomination.

EDIT: I actually just thought of a way involving references, but it's not a solution worth using so I don't want to post it.
/EDIT

This really is just a misuse of inheritance. There's no point in deriving from std::pair, here. If you want your vars named Lattitude and Longitude, then name them so:

1
2
3
4
5
struct LongitudeLatitude
{
    double Longitude;
    double Latitude;
};
Last edited on
Yea, with reference. I agree macro is an abomination.

So what do we do?

double & Longitude;

Then define it

double & Longitude = first;

That would work?

Well, yea I've been thinking about it my self, why do I need to derive things from std:pair anyway?

But in vb.net there is already a struct called point.
Basically yeah. You'd make Latitude and Longitude member vars as references and then seat them to first and second in the constructor.

But that's just more code, more confusion, and worse performance. You're much better off just not inherting from pair and making your own vars.
Topic archived. No new replies allowed.