title: Making a single pointer point to an array of pointers and pass for both C++
body: <p>I have a struct like this one:</p>
  
  <pre><code>struct foo
  {
      IBar* ptr;
  };
  </code></pre>
  
  <p>which is part of a foo object with a long lifetime. Users often get instances of those objects in different callbacks and sometimes they inject stuff into the IBar* in certain places, use it in later callbacks, free it eventually. (IBar has <code>virtual void free() { delete this; }</code> which can be overridden of course).</p>
  
  <p>The problem is I want to transition my users to the possibility of having more than one <code>IBar*</code> per foo struct and I want to make the transition smooth. One idea for making the transition smooth is changing the foo struct as follows:</p>
  
  <pre><code>struct foo
  {
      foo()
      {
          ptr = reinterpret_cast&lt;IBar*&gt;(new IBar*[N]);
          memset(ptr, 0, sizeof(IBar*)*N);
      }
  
      IBar*&amp; getBarPtr(size_t offset)
      {
          return reinterpret_cast&lt;IBar**&gt;(ptr)[offset];
      }
  
      IBar* ptr;
  };
  </code></pre>
  
  <p>My idea is that this way, anyone using the old style where they expect there to be only one <code>IBar*</code> should fallback on using the first of the <code>N</code> transparently in the usual way of using it:</p>
  
  <pre><code>someFoo.ptr = new iImplementIBar(...);
  </code></pre>
  
  <p>but those users who begin to transition to new usage can get their offset and instead do</p>
  
  <pre><code>someFoo.getBarPtr(myOffset) = new iImplementIBar(...);
  </code></pre>
  
  <p>the problem, though, is that <code>foo</code> had a <code>reset</code> method which used to do this:</p>
  
  <pre><code>void reset() {
      if (ptr)
      {
          ptr-&gt;free();
          ptr = 0;
      }
  }
  </code></pre>
  
  <p>which this idea replaces with this:</p>
  
  <pre><code>void reset() {
      IBar** ptrs = reinterpret_cast&lt;IBar*&gt;(ptr);
      for (size_t i = 0; i &lt; N; ++i)
           if (ptrs[i])
          {
              ptrs[i]-&gt;free();
              ptrs[i] = 0;
          }
  }
  </code></pre>
  
  <p>and the functionality of the <code>free</code> in the above seems to be lost. Is there a way to make this work? Or what goes wrong?</p>