title: Is it safe to pass pointers to data members to base class constructor?
body: <p>Is it safe to pass pointers to data members to base class constructor? i.e., is the memory layout of the derived class data members, at least, already set before the base class constructor is called, even if the data members are not yet initialized?</p>
  
  <p>Obviously, dereferencing the pointers must only be done after construction has completed, and the objects pointed to are already valid. But the question is if it's guaranteed that the pointers received by the base class constructor would actually still point to their objects once construction of the derived object has completed.</p>
  
  <p>The motivation is providing some functionality in the base class, for example iterating over the pointers-to-objects provided at construction and do something for each at a later time.</p>
  
  <p>It's possible to just provide a setter accessible to the derived class, but I'm curious if it's also safe to provide the pointers at construction time.</p>
  
  <p>Example:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;utility&gt;
  #include &lt;vector&gt;
  
  struct Base {
     Base(std::initializer_list&lt;int*&gt; ptrs = {}) : ptrs_(ptrs) {}
     std::vector&lt;int*&gt; ptrs_;
  };
  
  struct Derived : public Base {
      Derived() : Base{{&amp;a_,&amp;b_,&amp;c_}} {}
      int a_=1, b_=2, c_=3;
  };
  
  int main()
  {
      Derived obj;
      for (auto* ptr : obj.ptrs_) { std::cout &lt;&lt; *ptr &lt;&lt; '\n'; }
  }
  </code></pre>
  
  <p><a href="https://wandbox.org/permlink/rDJw0UU8KcWckLlo" rel="nofollow noreferrer">https://wandbox.org/permlink/rDJw0UU8KcWckLlo</a></p>