title: New/delete operator overload and base class
body: <p>whI have a big headache at the moment.</p>
  
  <p>Basically I have this :</p>
  
  <pre><code>Class A -&gt; Class B
  Class A -&gt; Class C
  Class A -&gt; Class D
  
  Class E with constructor with declaration E(int, A *objptr, bool IsObjPtrOwner = true)
  </code></pre>
  
  <p>As you see B C and D inherit from A, A being the base class. Class D must have a specific alignment (because SSE2 is used inside it), hence why I overloaded new/delete inside it, providing an aligned block of memory each time the object is dynamically allcated. Should also I mention since A B C and D differ, I would pretty much guess B C A and D do not have the same size at all.</p>
  
  <p>So I would like to be able to do that:</p>
  
  <pre><code>E eobj(12, new D(2.001), true);
  </code></pre>
  
  <p>Because the parameter IsObjPtrOwner would be true, I programmed my class E so that, if the member m_IsObjPtrOwner is true, the pointer m_objptr is deleted inside the destructor of E.</p>
  
  <p>This would be very convenient for me to be able to dynamically allocate one of the derived classes of A directly while constructing the object E, and not have to care about it later. The reason for this is that I will be making a lot of instances of class E throughout my program, each time with a different B/C/D instance. So I would like not to have to keep a copy of each pointer I create each time I construct an instance of E.</p>
  
  <p>So I tried to make new/delete operators pure virtual, but it just won't work. The damn functions must be static, very bothering. So I tried to circumvent this as much as I could, but I ended up discovering I cannot use "this" inside static functions.</p>
  
  <p>What can I do? How can I realize this dream? I have a dream...</p>
  
  <hr>
  
  <p>EDIT: For some reasons people do not understand at all what I'm trying to say.</p>
  
  <p>I have a base class A and a set of derived classes B/C/D from A. I have a class E taking as an argument in its constructor a pointer of class A, which is then stored in a member, say m_bcdobj, so I have this :</p>
  
  <pre><code>class B : public A {
      B(double x) : m_x(x) { bla bla bla}
      void *operator new(size_t size)   { return Util_MemAlign(size, 4); }
      void  operator delete(void* ptr)  { Util_AlignFree(ptr); }
  }
  class C : public A {
      C(double x) : m_x(x) { bla bla bla}
      void *operator new(size_t size)   { return malloc(size); }
      void  operator delete(void* ptr)  { free(ptr); }
  }
  class D : public A {
      D(double x) : m_x(x) { bla bla bla}
      void *operator new(size_t size)   { return Util_MemAlign(size, 16); }
      void  operator delete(void* ptr)  { Util_AlignFree(ptr); }
  }
  </code></pre>
  
  <p>As you see, each of em have different alignment requirements.</p>
  
  <p>Now I have a class E:</p>
  
  <pre><code>class E {
      public:
          E(int z, A *bcdobj, bool IsObjPtrOwner = true) : m_z(z), m_bcdobj(bcdobj), m_freebcd(IsObjPtrOwner) { bla bla bla }
  
          ~E() { if (m_freebcd) { delete m_bcdobj; } }
  
      private:
          A *m_bcdobj;
          int m_z;
          bool m_freebcd;
  }
  </code></pre>
  
  <p>So I want to be able to do that:</p>
  
  <pre><code>E einst(2, new D(2.001));
  </code></pre>
  
  <p>i.e. I don't keep a copy of the D object allocated. The allocated D object would be freed while "einst" is destroyed. The problem is that this code will not work. When deleting m_bcdobj inside ~E(), the overloaded delete operator inside D will not be called.</p>
  
  <p>Thanks!</p>