Re: use of pair<> for point?
todma wrote:
Should I use a pair<int,int> pPoint; .... or whatever inside of <>
instead of a POINT pPoint;..?
If so, how should I refer to the x,y, instead of saying pPoint.first
and pPoint.second?
Maybe I would use pair as a private base class, as POINT "WORKS-AS-A"
pair and it could use pair only for its implementation.
Moreover, pair has no virtual destructor so it's not intended for
public derivation and polymorphic use.
I would then add "getter" and "setter" overloaded methods with the
names x and y, of course more expressive of first and second in a
"point" context. I prefer methods over direct access to fields to
respect the "Uniform Access Principle", other than encapsulation.
A sketch of the code is:
class POINT
: private pair<int,int>
{
public:
...// constructor(s)
int x() const { return first; }
int y() const { return second; }
void x(int new_x) { first = new_x; }
void y(int new_y) { first = new_y; }
};
But now I have a couple of questions:
1. Is a good idea to add a conversion operator POINT::operator
pair<int,int>()? With my choice of private inheritance, a POINT is not
a substitute for pair, so I can't access to operators and other
functions defined for pair.
2. Is this kind of reuse worth? pair is so simple as a class, that I
ask myself where is the ...point :-) in using it as a base for POINT.
Best regards,
Umberto (happy for his 1st post to this group)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]