C++ question about alias element names in classes or structs

So I have this nice class that handles a pair of values. Let’s call them “a and b”. Sometimes I want them to be coordinates, in which case I’d prefer the names “x and y”. Other times I’d like them to be dimensions called “height and width”. How can I do this without re-defining the class three times?

Thanks
Norm

You would not. You are on a path fraught with peril. But, who am I to deny you the rope with which to hang yourself. Use a union or accessors.

Why do X & Y not suffice? If the object is called size, then X is a clear enough name for width. Say a point structure:

struct Point{
  int X;
  int Y;
}

//typedef and just use X, Y
typedef Point Size;

//Or derive point and add accessors
struct Size : Point{
  int Height() { return Y; }
  int Width() { return X; }
};

TheNorm:
Sometimes I want them to be coordinates, in which case I'd prefer the names "x and y". Other times I'd like them to be dimensions called "height and width".

In OO design terms these things (size and position) represent completely different concepts and should be represented by separate classes/types. The fact they both consist of pairs of values is not significant.

If there's something you're doing to both types of data which treats them the same way then you could define a common base class representing a value pair, and use that for the code that needs to deal with both types. (There are lots of other ways to deal with that, some of which would work much better in more complex situations, but I think the minimal approach is all you need here.)

...define a common base class representing a value pair, and use that for the code that needs to deal with both types.

That's how it ended up. I haven't added the weird storage options I had in mind yet but it should be relatively painless.

Thanks all