title: C++ addition operator overloading multiple times
body: <p>i am trying to get some mathematical construct working which contains 'Cells' which represent a mathematical function with a set of inputs and outputs.</p>
  <p>Each mathematical operation gets their own class which inherits from <code>Cell</code>. Using some operator overloading, I would like to spawn a new <code>Add</code>-Cell when i call <code>operator+</code> on two cells.</p>
  <p>This works fine so far but I encountered a problem.</p>
  <p>I have written a small demo code which illustrates my problem:</p>
  <p>Imagine this class which fully implements copy/move assignment/construction as well as a custom destructor and a constructor.</p>
  <pre class="lang-cpp prettyprint-override"><code>
  class Cell{
  
  private:
      Cell* A = nullptr;
      Cell* B = nullptr;
  public:
      Cell() {
          std::cout &lt;&lt; &quot;default constructed&quot; &lt;&lt; std::endl;
      }
      Cell(Cell&amp; A, Cell&amp; B) {
          this-&gt;A = &amp;A;
          this-&gt;B = &amp;B;
          std::cout &lt;&lt; &quot;default constructed&quot; &lt;&lt; std::endl;
      }
      T(const Cell&amp; other){
          this-&gt;A = other.A;
          this-&gt;B = other.B;
          std::cout &lt;&lt; &quot;copy constructed&quot; &lt;&lt; std::endl;
  
      }
      T(Cell&amp;&amp; other){
          this-&gt;A = other.A;
          this-&gt;B = other.B;
          std::cout &lt;&lt; &quot;move constructed&quot; &lt;&lt; std::endl;
  
      }
      Cell&amp; operator=(Cell&amp;&amp; other){
          this-&gt;A = other.A;
          this-&gt;B = other.B;
          std::cout &lt;&lt; &quot;move assigned&quot; &lt;&lt; std::endl;
          return *this;
  
      }
      Cell&amp; operator=(const Cell&amp;&amp; other){
          this-&gt;A = other.A;
          this-&gt;B = other.B;
          std::cout &lt;&lt; &quot;copy assigned&quot; &lt;&lt; std::endl;
          return *this;
      }
  
      Cell operator+(Cell&amp; other){
          return T{*this, other};
      }
  
      virtual ~Cell() {
          std::cout &lt;&lt; &quot;destructed&quot; &lt;&lt; std::endl;
      }
  
  };
  
  </code></pre>
  <p>It is similar to my code. Here <code>Cell</code> can be considered any type of cell with 2 previous mathematical operations.</p>
  <p>So the issue arises when I try to do something like this:</p>
  <pre class="lang-cpp prettyprint-override"><code>    Cell t1{};
      Cell t2{};
      Cell t3{};
  
      auto h = t1 + t2 + t3;
  
  </code></pre>
  <p>It creates 3 empty <code>Cell</code> objects. Then it creates a new <code>Cell</code> based on <code>t1</code> and <code>t2</code> and uses that to create a new <code>Cell</code> together with <code>t3</code>. The issue here is that the first <code>Cell = t1 + t2</code> will be destructed directly after that operation and the final tree will hold an invalid reference.</p>
  <p>This problem could easily be overcome by doing this:</p>
  <pre class="lang-cpp prettyprint-override"><code>    auto h1 = t1 + t2;
      auto h  = h1 + t3;
  </code></pre>
  <p>or using <code>new</code> inside the overloaded addition operator <code>operator+</code>.</p>
  <hr />
  <p>I would like to not use <code>new</code> inside the operator since cleaning them up eventually could only be achieved by using some sort of tracking system.</p>
  <p>I was wondering if there is a nice solution to this problem.</p>
  <p>I am very happy for any advice or help.</p>