Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your implementation is not good : the <code>operator=</code> should return reference to object of type Array, and should not be virtual :</p> <pre><code>Array &amp;Array::operator=(const DataStructure &amp;other) { DataStructure::operator=(other); return *this; } </code></pre> <p>You can change <code>DataStructure</code> to use <a href="http://en.wikipedia.org/wiki/Non-virtual_interface_pattern" rel="nofollow">NVI</a> :</p> <pre><code>#include &lt;iostream&gt; class DataStructure { public: DataStructure(){} inline virtual ~DataStructure(){} DataStructure &amp;operator=(const DataStructure &amp; other); inline friend const DataStructure &amp;operator+(const int a, const DataStructure &amp; other) { other.add(a); return other; } //... private: virtual void add( const int a ) const = 0; }; struct Array : DataStructure { virtual void add( const int a ) const { std::cout&lt;&lt;"adding "&lt;&lt;a&lt;&lt;std::endl; } }; void foo(const DataStructure &amp;a) { const DataStructure &amp;b = 5 + a; } int main() { Array a; foo(a); } </code></pre> <p>Check <a href="http://codepad.org/LaCMeP65" rel="nofollow">live demo</a>. Then you have to implement the method <code>add</code> in your derived class Array.</p> <hr> <p>To your edit :</p> <p>Your new idea is an awful way of doing things in c++. What you do there is tell your compiler : "stop complaining, I know what I am doing". Also, it causing an undefined behaviour. It may appear to work, until you application starts crashing one day.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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