Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can (but shouldn't<sup>*</sup>) use a proxy class that overloads the conversion operators. </p> <h1>Long example with actual usecase <sup>*</sup></h1> <p>Let me take my example from <a href="http://ompf.org/forum/viewtopic.php?f=11&amp;t=1453" rel="nofollow">Dot &amp; Cross Product Notation</a>:</p> <p>[...]</p> <p>There is also the possibility of having operator* for both dot-product and cross-product.</p> <p>Assume a basic vector-type (just for demonstration):</p> <pre><code>struct Vector { float x,y,z; Vector() {} Vector (float x, float y, float z) : x(x), y(y), z(z) {} }; </code></pre> <p>We observe that the dot-product is a scalar, the cross-product is a vector. In C++, we may overload conversion operators:</p> <pre><code>struct VecMulRet { public: operator Vector () const { return Vector ( lhs.y*rhs.z - lhs.z*rhs.y, lhs.z*rhs.x - lhs.x*rhs.z, lhs.x*rhs.y - lhs.y*rhs.x ); } operator float () const { return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z; } private: // make construction private and only allow operator* to create an instance Vector const lhs, rhs; VecMulRet (Vector const &amp;lhs, Vector const &amp;rhs) : lhs(lhs), rhs(rhs) {} friend VecMulRet operator * (Vector const &amp;lhs, Vector const &amp;rhs); }; </code></pre> <p>Only operator* is allowed to use struct VecMulRet, copying of VecMulRet is forbidden (paranoia first).</p> <p>Operator* is now defined as follows:</p> <pre><code>VecMulRet operator * (Vector const &amp;lhs, Vector const &amp;rhs) { return VecMulRet (lhs, rhs); } </code></pre> <p>Et voila, we can write:</p> <pre><code>int main () { Vector a,b; float dot = a*b; Vector cross = a*b; } </code></pre> <p>Btw, this is blessed by the Holy Standard as established in 1999.</p> <p>If you read further in that thread, you'll find a benchmark that confirms that this comes at no performance penalty.</p> <h1>Short example for demonstration <sup>*</sup></h1> <p>If that was too much to grasp, a more constructed example:</p> <pre><code>struct my_multi_ret { operator unsigned int() const { return 0xdeadbeef; } operator float() const { return 42.f; } }; my_multi_ret multi () { return my_multi_ret(); } #include &lt;iostream&gt; #include &lt;iomanip&gt; int main () { unsigned int i = multi(); float f = multi(); std::cout &lt;&lt; std::hex &lt;&lt; i &lt;&lt; ", " &lt;&lt; f &lt;&lt; std::endl; } </code></pre> <hr> <p><sup>*</sup> You can, but shouldn't, because it does not conform to the principle of least surprise as it is not common practice. Still, it is funny.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload