title: Access Parent Class from member without storing pointer of Parent
body: <p>Is it possible to safely access the parent class from a member without the need to store the pointer of the parent class in the member? Something like a this pointer but just a parent pointer?</p>
  
  <pre><code>#include &lt;cstddef&gt;
  #include &lt;cstdint&gt;
  
  #include &lt;iostream&gt;
  
  template&lt; class Member, class Parent, ptrdiff_t parentToMemberOffset&gt;
  struct ParentAccess
  {
    Parent &amp; parent() { return *reinterpret_cast&lt;Parent*&gt;(reinterpret_cast&lt;char*&gt;(static_cast&lt;Member*&gt;(this)) - parentToMemberOffset); }
  };
  
  template&lt;class Parent, ptrdiff_t parentToMemberOffset&gt;
  struct A : ParentAccess&lt;A&lt;Parent,parentToMemberOffset&gt;, Parent, parentToMemberOffset&gt;
  {
    void notifyParent() { this-&gt;parent().notify(); }
  };
  
  struct Host
  {
    uint64_t counter = 123;
    A&lt;Host, 8 /*what safe here?*/&gt; a;
  
    void notify() { counter++; std::cout &lt;&lt; "counter is " &lt;&lt; counter; }
  };
  
  int main()
  {
      Host h;
      h.a.notifyParent();
      return 0;
  }
  </code></pre>
  
  <p><a href="https://ideone.com/Nps2Ye" rel="nofollow noreferrer">https://ideone.com/Nps2Ye</a></p>