title: Can virtual functions be realized by implicit type conversion? Or how can I do a subclass-dependent type conversion?
body: <p>Consider two classes <code>class B</code> and <code>class C</code>, each having one <code>int</code> type <code>private</code> member, a constructor that sets the <code>int</code> member to zero, and an <code>operator++()</code> member function, such that <code>B b; ++b</code> increases the <code>int</code> member by 1, and <code>C c; ++c</code> increases the member by 10. This is then a classic example of polymorphism. We can derive <code>class B</code> and <code>class C</code> from a common abstract base <code>class A</code>, which has a pure virtual member function <code>operator++()=0</code>. When the concrete <code>operator++()</code> functions are defined for both <code>class B</code> and <code>class C</code>, we can use <code>A&amp; a=b; ++a</code> to increase (the <code>int</code> member of) <code>a</code> by 1, and <code>A&amp; a=c; ++a</code> to increase <code>a</code> by 10.</p>
  
  <p>Now the situation is slightly more complicated. Suppose we have</p>
  
  <pre><code>  class B{
      private:
        int n;
      public:
        B(int b=0) {n=b;}
        void output() {cout&lt;&lt;n&lt;&lt;endl;}
        operator int&amp;() {return n;}
    };
  
    class C{
      private:
        int n;
      public:
        C(int c=0) {n=c;}
        void output() {cout&lt;&lt;n&lt;&lt;endl;}
        C&amp; operator++() {n+=10; return *this;}
    };
  </code></pre>
  
  <p>such that the <code>B b; ++b</code> operation is not done by a member function <code>operator++()</code>, but via an implicit type conversion of <code>b</code> to an <code>int&amp;</code> reference to its internal <code>int</code> member and then the <code>++</code> operator of type <code>int</code>. Then I find that polymorphism no longer works.</p>
  
  <pre><code>  class A{
      protected:
        int n;
      public:
        A(int a=0) {n=a;}
        void output() {cout&lt;&lt;n&lt;&lt;endl;}
        virtual A&amp; operator++()=0;
    };
  
    class B:public A{
      public:
        operator int&amp;() {return n;}
    };
  
    class C:public A{
      public:
        C&amp; operator++() {n+=10; return *this;}
    };
  </code></pre>
  
  <p>The above code only works for <code>A&amp; a=c; ++a</code> but not <code>A&amp; a=b; ++a</code>. The pure virtual <code>operator++()=0</code> of the common base <code>class A</code> is inherited to <code>class B</code> and remains purely virtual, so that <code>class B</code> becomes abstract and can no longer define any objects, even if <code>++b</code> is in fact doable via the type conversion.</p>
  
  <p>Suppose the class-dependent type conversion is some feature I want, because maybe the conversion is expensive and I don't want it to happen to <code>class C</code>, but somehow I have no choice but to do it for <code>class B</code>. I want my code to be smart by using some kind of "polymorphism" (even though a direct one fails here). Does anybody see a way to do this subclass-dependent type conversion in C++? I mean, can it happen that when I do <code>++a</code>, it is converted to <code>int</code> and then <code>++</code>ed if <code>a</code> is a <code>B</code>, but is directly <code>++</code>ed by the member function if <code>a</code> is a <code>C</code>?</p>