title: c++ Overload operator in abstract class
body: <p>I have the folllowing interface:</p>
  
  <pre><code>class A
  {
    public:
      virtual A * operator+(const A &amp;rhs) const = 0;
  }
  </code></pre>
  
  <p>And the abstract class :</p>
  
  <pre><code>class B : public A
  {
    public:
      B(int val)
      {
        this-&gt;val = val;
      }
  
      virtual A * operator+(const A &amp;rhs) const
      {
        return (new B(this-&gt;val + rhs.val));
      }
      int val;
  }
  </code></pre>
  
  <p>Moreover, I have this class :</p>
  
  <pre><code>class C
  {
    public:
      void add();
    private:
      std::stack&lt;A *&gt; Astack;
  }
  </code></pre>
  
  <p><strong>The operator+ prototype cannot be modified.</strong></p>
  
  <p>My issue is that I fails to create the add function. I tried this:</p>
  
  <pre><code>void    C::add()
  {
    B first = *dynamic_cast&lt;B *&gt;(this-&gt;Astack.top()); // Error here
    this-&gt;Astack.pop();
    B second = *dynamic_cast&lt;B *&gt;(this-&gt;Astack.top()); // And here
    this-&gt;Astack.pop();
    B * res = first + second;
    this-&gt;Astack.push(res);
  }
  </code></pre>
  
  <p>But my compiler tells me :
  error: cannot convert <code>B</code> to <code>A *</code> in initialization.
  In fact, I fails to obtain to <code>B</code> to add them.</p>