title: Copying a reference to an uninitialized object in c++
body: <p>According to the c++ standard, is it undefined behavior to copy a reference before initializing the object it refers to? This happens in the following example, where I pass a reference to a parent class and I initialize the value of the object only after because the call to the parent constructor has to be first in the initializer list. </p>
  
  <pre><code>#include &lt;iostream&gt;
  
  struct Object
  {
    int val;
    Object(int i): val(i) {}
  };
  
  struct Parent 
  {
    Object&amp; ref;
    Parent(Object&amp; i): ref(i){}
  };
  
  struct Child : Parent
  {
    Object obj;
    Child(int i): Parent(obj), obj(i) {}
  };
  
  int main()
  {
    std::cout &lt;&lt; Child(3).ref.val;
  }
  </code></pre>
  
  <p>Here when <strong>Parent</strong> is initialized with Parent(obj), the value of <strong>obj</strong> has not been initialized yet. </p>
  
  <p>This compiles fine under gcc, and I do get a correct output, but I'm not sure whether the standard or good coding practice advise against it. Is it an undefined behavior ? And if not, is it a bad practice that I should avoid?</p>