<p>I'm using two different libraries in my project, and both of them supply a basic rectangle <code>struct</code>. 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.</p>

<p>library a:</p>

<pre><code>typedef struct rectangle { sint16 x; sint16 y; uint16 w; uint16 h; } rectangle;
</code></pre>

<p>library b:</p>

<pre><code>class Rect {
  int x; int y; int width; int height;
  /* ... */
};
</code></pre>

<p>Now, I can't make a converter <code>class</code>, 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.</p>

<p>I can't make an operator that takes the <code>struct</code> from <code>a</code> and supplies an object of the <code>class</code> from <code>b</code>:</p>

<pre>foo.cpp:123 error: ‘operator b::Rect(const rectangle&)’ must be a nonstatic member function</pre>

<p>So, is there a sensible way around this?</p>

<h2>edit:</h2>

<p>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, <a href="http://en.cppreference.com/w/cpp/language/reinterpret_cast" rel="nofollow noreferrer"><code>reinterpret_cast</code></a> has the same problem..)</p>

<h2>edit2:</h2>

<p>Actually, none of the suggestions really answer my actual question, <a href="https://stackoverflow.com/users/1968/konrad-rudolph">Konrad Rudolph</a> 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 <a href="https://stackoverflow.com/users/90/codingthewheel">CodingTheWheel</a>.</p>

[{'answer_id': 28223, 'author': 'dmckee --- ex-moderator kitten', 'author_id': 2509, 'author_profile': 'https://Stackoverflow.com/users/2509', 'pm_score': 0, 'selected': False, 'text': "<p>It may not be feasible in your case, but I've seen people employ a little preprocessor-foo to massage the types into compatibility.</p>\n\n<p>Even this assumes that you are building one or both libraries.</p>\n\n<p>It is also possible that you don't want to do this at all, but want to re-evaulate some early decision. Or not.</p>\n"}, {'answer_id': 28226, 'author': 'Stu', 'author_id': 414, 'author_profile': 'https://Stackoverflow.com/users/414', 'pm_score': 0, 'selected': False, 'text': '<p>If the <code>struct</code>s were the same internally, you could do a <a href="http://en.cppreference.com/w/cpp/language/reinterpret_cast" rel="nofollow noreferrer"><code>reinterpret_cast</code></a>; however, since it looks like you have 16-bit vs 32-bit fields, you\'re probably stuck converting on each call, or writing wrappers for all functions of one of the libraries.</p>\n'}, {'answer_id': 28232, 'author': 'mmattax', 'author_id': 1638, 'author_profile': 'https://Stackoverflow.com/users/1638', 'pm_score': 0, 'selected': False, 'text': "<p>Why not something simple like this: (note this may/probably won't compile) but you get the idea...</p>\n\n<pre>\n<code>\nprivate Rect* convert(const rectangle& src)\n{\n    return new Rect(src.x,src.y,src.w,src.h);\n}\nint main()\n{\n    rectangle r;\n    r.x = 1;\n    r.y = 2;\n    r.w = 3;\n    r.h = 4;\n\n    Rect* foo = convert(&r);\n    ...\n    delete foo;\n\n}\n</code>\n</pre>\n\n<p><strong>EDIT:</strong> Looks like koko's and I have the same idea.</p>\n"}, {'answer_id': 28239, 'author': 'bernhardrusch', 'author_id': 3056, 'author_profile': 'https://Stackoverflow.com/users/3056', 'pm_score': -1, 'selected': False, 'text': '<p>Maybe you could try it with operator overloading ? (Maybe a = operator which is not a method of your class) ?</p>\n\n<p>Rect operator= (const Rect&amp;,const rectangle&amp;)</p>\n\n<p>More about this in the C++ programming language by Bjarne Stroustrup or maybe on this page: <a href="http://www.cs.caltech.edu/courses/cs11/material/cpp/donnie/cpp-ops.html" rel="nofollow noreferrer">http://www.cs.caltech.edu/courses/cs11/material/cpp/donnie/cpp-ops.html</a></p>\n'}, {'answer_id': 28240, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>Create an intermediate shim type "<code>RectangleEx</code>", and define custom conversions to/from the 3rd-party string types. Whenever you speak to either API, do so through the shim class.</p>\n\n<p>Another way would be to derive a <code>class</code> from either <code>rect</code> or <code>Rectangle</code>, and insert conversions/constructors there.</p>\n'}, {'answer_id': 28244, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 3, 'selected': True, 'text': "<p>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.</p>\n"}, {'answer_id': 28246, 'author': 'Kristopher Johnson', 'author_id': 1175, 'author_profile': 'https://Stackoverflow.com/users/1175', 'pm_score': 2, 'selected': False, 'text': '<p>Not sure how sensible this is, but how about something like this:</p>\n\n<pre><code>class R\n{\npublic:\n    R(const rectangle&amp; r) { ... };\n    R(const Rect&amp; r) { ... };\n\n    operator rectangle() const { return ...; }\n    operator Rect() const { return ...; }\n\nprivate:\n    ...\n};\n</code></pre>\n\n<p>Then you can just wrap every <code>rectangle</code> in <code>R()</code> and the "right thing" will happen.</p>\n'}]
['https://Stackoverflow.com/questions/28212', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1421/']