I’m using two different libraries in my project, and both of them supply a basic rectangle struct. The problem with this is that there seems to be no way to insert a conversion between the types, so I can’t call a function in one library with the result from a function in the other. If I was the author of either of these, I could create conversions, from the outside, I can’t.
library a:
typedef struct rectangle { sint16 x; sint16 y; uint16 w; uint16 h; } rectangle;
library b:
class Rect { int x; int y; int width; int height; /* ... */ };
Now, I can’t make a converter class, because C++ will only look for a conversion in one step. This is probably a good thing, because there would be a lot of possibilities involving creating new objects of all kinds of types.
I can’t make an operator that takes the struct from a and supplies an object of the class from b:
foo.cpp:123 error: ‘operator b::Rect(const rectangle&)’ must be a nonstatic member function
So, is there a sensible way around this?
edit:
I should perhaps also point out that I’d really like some solution that makes working with the result seamless, since I don’t expect to be that coder. (Though I agree, old-school, explicit, conversion would have been a good choice. The other branch, reinterpret_cast has the same problem..)
edit2:
Actually, none of the suggestions really answer my actual question, Konrad Rudolph seems to be correct. C++ actually can’t do this. Sucks, but true. (If it makes any difference, I’m going to try subclassing as suggested by CodingTheWheel.
If you can’t modify the structures then you have no alternative to writing a manual conversion function because overloading conversion operators only works within the class body. There’s no other way.