Write a Point adapter class that has implicit conversion syntax for both target types. Note that data must be copied, therefore not ideal:
class Point { XType x; YType y; public: Point (const SomePoint& orig) : x(orig.X), y(orig.Y){} Point (const DifferentPoint& orig) : x(orig.x), y(orig.y){} XType getX(){return x;}; YType getY(){return y;}; }
This is not ideal, but if you cannot access the internal functions of the other two classes, this is a potential solution. Of course, I assumed that your X and Y were at the same time as X and Y ...
Use then
void printX (const Point& point) { std::cout << point.getX(); } ... SomePoint origin(0,0); printX(Point{origin});
The TartanLlama solution above is more flexible, but allows the use of different types of X and Y.
source share