title: How can I initialize C++ object member variables in the constructor?
body: <p>I've got a class that has a couple of objects as member variables. I don't want the constructors for these members to be called when declared, so I'm trying to hang onto a pointer to the object explicitly. I have no idea what I'm doing.</p>
  <p>I thought maybe I could do the following, where the constructor is called immediately when initializing the object member variable:</p>
  <pre><code>class MyClass {
      public:
          MyClass(int n);
      private:
          AnotherClass another(100); // Construct AnotherClass right away!
  };
  </code></pre>
  <p>But I want the <code>MyClass</code> constructor to call the <code>AnotherClass</code> constructor. Here's what my code looks like:</p>
  <h3>FIle <em>BigMommaClass.h</em></h3>
  <pre><code>#include &quot;ThingOne.h&quot;
  #include &quot;ThingTwo.h&quot;
  
  class BigMommaClass {
  
          public:
                  BigMommaClass(int numba1, int numba2);
  
          private:
                  ThingOne* ThingOne;
                  ThingTwo* ThingTwo;
  };
  </code></pre>
  <h3>FIle <em>BigMommaClass.cpp</em></h3>
  <pre><code>#include &quot;BigMommaClass.h&quot;
  
  BigMommaClass::BigMommaClass(int numba1, int numba2) {
          this-&gt;ThingOne = ThingOne(100);
          this-&gt;ThingTwo = ThingTwo(numba1, numba2);
  }
  </code></pre>
  <p>Here's the error I'm getting when I try to compile:</p>
  <pre class="lang-none prettyprint-override"><code>g++ -Wall -c -Iclasses -o objects/BigMommaClass.o classes/BigMommaClass.cpp
  In file included from classes/BigMommaClass.cpp:1:0:
  classes/BigMommaClass.h:12:8: error: declaration of âThingTwo* BigMommaClass::ThingTwoâ
  classes/ThingTwo.h:1:11: error: changes meaning of âThingTwoâ from âclass ThingTwoâ
  classes/BigMommaClass.cpp: In constructor âBigMommaClass::BigMommaClass(int, int)â:
  classes/BigMommaClass.cpp:4:30: error: cannot convert âThingOneâ to âThingOne*â in assignment
  classes/BigMommaClass.cpp:5:37: error: â((BigMommaClass*)this)-&gt;BigMommaClass::ThingTwoâ cannot be used as a function
  make: *** [BigMommaClass.o] Error 1
  </code></pre>
  <p>Am I using the right approach, but the wrong syntax? Or should I be coming at this from a different direction?</p>