Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ operator overloading with inheritance
    text
    copied!<p>Let's say I have a class called <code>Vehicle</code> and another called <code>Car</code> that extends <code>Vehicle</code> class. I want to implement <code>++</code> operators for both classes. </p> <pre><code>#include &lt;cstdio&gt; #include &lt;cmath&gt; #include &lt;cstring&gt; #include &lt;ctime&gt; #include &lt;iostream&gt; #include &lt;algorithm&gt; #include &lt;set&gt; #include &lt;map&gt; #include &lt;vector&gt; #include &lt;sstream&gt; #include &lt;typeinfo&gt; #define debug(args...) // Just strip off all debug tokens using namespace std; // CUT begin #define debug(args...) {dbg,args;cout&lt;&lt;endl;} struct debugger{template&lt;typename T&gt; debugger&amp; operator ,(const T&amp; v){cout&lt;&lt;v&lt;&lt;" ";return *this;}}dbg; template &lt;typename T1,typename T2&gt; inline ostream&amp; operator&lt;&lt;(ostream&amp; os,const pair&lt;T1,T2&gt;&amp; p){return os&lt;&lt;"("&lt;&lt;p.first&lt;&lt;", "&lt;&lt;p.second&lt;&lt;")";} template&lt;typename T&gt;inline ostream&amp;operator&lt;&lt;(ostream&amp; os,const vector&lt;T&gt;&amp; v){string delim="[";for(unsigned int i=0;i &lt; v.size();i++){os&lt;&lt;delim&lt;&lt;v[i];delim=", ";}return os&lt;&lt;"]";} template&lt;typename T&gt;inline ostream&amp;operator&lt;&lt;(ostream&amp; os,const set&lt;T&gt;&amp; v){string delim="[";for (typename set&lt;T&gt;::const_iterator ii=v.begin();ii!=v.end();++ii){os&lt;&lt;delim&lt;&lt;*ii;delim=", ";}return os&lt;&lt;"]";} template&lt;typename T1,typename T2&gt;inline ostream&amp;operator&lt;&lt;(ostream&amp; os,const map&lt;T1,T2&gt;&amp; v){string delim="[";for (typename map&lt;T1,T2&gt;::const_iterator ii=v.begin();ii!=v.end();++ii){os&lt;&lt;delim&lt;&lt;*ii;delim=", ";}return os&lt;&lt;"]";} // CUT end class Vehicle { public: int n; Vehicle(int n):n(n){cout&lt;&lt;"Ctor Vehicle "&lt;&lt;n&lt;&lt;endl;} Vehicle(Vehicle&amp; v):n(v.n){cout&lt;&lt;"Copy Ctor Vehicle "&lt;&lt;n&lt;&lt;endl;} virtual ~Vehicle(){cout&lt;&lt;"Dtor Vehicle "&lt;&lt;n&lt;&lt;endl;} virtual ostream&amp; dump(ostream&amp; os){return os&lt;&lt;"Vehicle("&lt;&lt;n&lt;&lt;")";} string to_str(){stringstream s; dump(s); return s.str();} virtual Vehicle&amp; operator++(){n++;return *this;} virtual Vehicle operator++(int x){Vehicle v(*this); operator++(); return v;} }; class Car: public Vehicle { public: Car(int n): Vehicle(n){cout&lt;&lt;"Ctor Car "&lt;&lt;n&lt;&lt;endl;} virtual ~Car(){cout&lt;&lt;"Dtor Car "&lt;&lt;n&lt;&lt;endl;} virtual ostream&amp; dump(ostream&amp; os){return os&lt;&lt;"Car("&lt;&lt;n&lt;&lt;")";} virtual Car operator++(int x){Car v(*this); operator++(); return v;} /* data */ }; ostream&amp; operator&lt;&lt;(ostream&amp; os, Vehicle&amp; v) { return v.dump(os); } int main(int argc, char const *argv[]) { Vehicle * v = new Car(10); // cout&lt;&lt;c++&lt;&lt;endl; // cout&lt;&lt;c&lt;&lt;endl; return 0; } </code></pre> <p>I get the following error with gcc:</p> <pre><code>C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:16:0: warning: "debug" redefined [enabled by default] C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:13:0: note: this is the location of the previous definition C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:44:14: error: invalid covariant return type for 'virtual Car Car::operator++(int)' C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:35:18: error: overriding 'virtual Vehicle Vehicle::operator++(int)' C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp: In member function 'virtual Car Car::operator++(int)': C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:44:57: error: no matching function for call to 'Car::operator++()' C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:44:57: note: candidate is: C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:44:14: note: virtual Car Car::operator++(int) C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:44:14: note: candidate expects 1 argument, 0 provided </code></pre> <p>How do I get <code>++</code> operators for both <code>Car</code> and <code>Vehicle</code> with minimum number of virtual overrides?</p>
 

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