Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Eventually I managed to find an answer. For <code>operator=</code>:</p> <pre class="lang-cpp prettyprint-override"><code>class Array : public DataStructure { public: //... using DataStructure::operator=; Array &amp;operator=(const DataStructure &amp;); }; </code></pre> <p>Writing only <code>Array &amp;operator=(const DataStructure &amp;);</code> would hide <code>DataStructure::operator=</code>, but with the <code>using</code> statement it only changes the method's return type.</p> <p><em>Note:</em> the <code>using</code> statement must precede the line <code>Array &amp;operator=(const DataStructure &amp;);</code>, otherwise <code>operator=</code> would be ambiguous.</p> <hr/> <p>As for <code>operator+</code>, the only solution I found is very problematic. It uses templates:</p> <pre class="lang-cpp prettyprint-override"><code>public DataStructure { public: template&lt;class T&gt; friend const T &amp;operator+(int, const T &amp;); }; </code></pre> <p>For instance, if the original implementation was</p> <pre class="lang-cpp prettyprint-override"><code>const DataStructure &amp;operator+(int x, const DataStructure &amp;other) { DataStructure addition(other); /* in this example we assume * that "DataStructure" isn't abstract */ addition.someParameter += x; /* for simplicity, assume that * "someParameter" is public/protected */ return addition; } </code></pre> <p>and we want to achieve the same result as in</p> <pre class="lang-cpp prettyprint-override"><code>const Array &amp;operator+(int x, const Array &amp;other) { return (const Array &amp;)(x + (DataStructure)other); } </code></pre> <p>than we can simply write:</p> <pre class="lang-cpp prettyprint-override"><code>template&lt;class T&gt; const T &amp;operator+(int x, const T &amp;other) { DataStructure addition(other); /* notice that usually we would want * to write "T" instead of "DataStructure", but sometimes it would * give a different result */ addition.someParameter += x; return (const T &amp;)addition; } </code></pre> <p>However, if there exists a function <code>operator+(int,/*const*/ ThirdClass /*&amp;*/)</code> that has nothing to do with <code>DataStructure</code> or <code>Array</code> than we would have to change it, which might mess up the code. Luckily there are usually just a few friend functions, so implementing a new method that performs a conversion (as shown in the question) isn't so bad.</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.
 

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