title: Get pointer to self inside class
body: <p>How to get pointer to self object inside the class?
  Simple code below shows the creation of two objects: A and B. 
  A contains pointer to B that must me assigned in B's constructor.</p>
  
  <pre><code>#include&lt;iostream&gt;
  
  using namespace std;
  
  class B;
  class A {
      public:
      B* ptr;
  };
  
  class B {
      char array[1024];
      public:
  
      int value = 12;
  
      B() {};
  
      B(A* a) {
          a-&gt;ptr = this;
          cout &lt;&lt; "constructor B addr = " &lt;&lt; reinterpret_cast&lt;std::uintptr_t&gt;(this) &lt;&lt; endl;
      }
  };
  
  int main() {
      B b;
      A a;
  
      b = B(&amp;a);
  
      cout &lt;&lt; "stack B addr       = " &lt;&lt; reinterpret_cast&lt;std::uintptr_t&gt;(&amp;b) &lt;&lt; endl;
  
      a.ptr-&gt;value = 10;
      cout &lt;&lt; "B.value            = " &lt;&lt; b.value &lt;&lt; endl;
  
      return 0;
  };
  </code></pre>
  
  <p>Example output of this code seems to be logically incorrect:</p>
  
  <pre><code>constructor B addr = 140737243484752
  stack B addr       = 140737243483712
  B.value            = 12
  </code></pre>
  
  <p>So, is there any variant to get pointer to an object inside itself?</p>