title: Initialising classes inside another class in C++?
body: <p>I have this definition in a header file:</p>
  
  <pre><code>class Owner
  {
  private:
      // Fields
      Child* _myChild1;
  public:
      // Constructors
      Owner();
      Owner(const char childName[]);
  };
  </code></pre>
  
  <p>and this implementation:</p>
  
  <pre><code>Owner::Owner(const char childName[])
  {
  //do some operations - children must be created after these ops
  _myChild = new Child(childName);
  }
  </code></pre>
  
  <p>and this main() function:</p>
  
  <pre><code>int main()
  {
  Owner("child1"); 
  }
  </code></pre>
  
  <p>Some questions, and please bear with me here, I'm just starting out with C++ ..</p>
  
  <ul>
  <li>Given that the child classes are known at compile time am I right in thinking that I don't need to create them with 'new' on the heap? If so how? I have tried using this syntax in the Owner implementation but the compiler moans ('term does not evaluate to a function..'):</li>
  </ul>
  
  <p><code>_myChild(childName);</code></p>
  
  <ul>
  <li>However, using this syntax in the implementation is ok, why?</li>
  </ul>
  
  <p><code>Child _myChild(childName);</code></p>
  
  <ul>
  <li>Is the paradigm that I'm using correct? In other words, as a general rule, if one class wraps another does the owner only ever hold pointers to the classes it wraps? </li>
  <li>How would you more experienced guys do it?</li>
  </ul>
  
  <p>Thanks for any advice..</p>