Creating c++ object on the stack, trying hard not to ever allocate.
Suppose I have a class <pre>
    class A {
      public:
      A(int i);
      ~A();
      private:
      B b;   // Want <- this guy to be constructed with i in A's constructor!
    };
</pre>
I want b to be constructed in the constructor with particular parameters that aren't known until A is constructed. If I were to do the following in A's constructor:
<pre>
    A::A(int i) {   
      B b(i);
      // Or even if I try to do b = B::B(i);
    }
</pre>
I notice that b get's allocated twice on the stack! aghghg.

Then I found out that what I can do in A's constructor is:
<pre>
A::A() : b(B::B(7)) {

}</pre>
And b only gets allocated on the stack once!

But this is pretty clunky. Anyone got a better idea? Remember, the constructor should only be called once!

Is this the standard way of allocating objects NON-dynamically with important parameters? What if we can shove b's construction into that fancy argument list thing!? You're forced to either dynamically allocate, or construct TWICE on the stack!

Bonus Question: When does b get deallocated? Is it after or right before A's destructor
Creating c++ object on the stack, trying hard not to ever allocate.
===

Suppose I have a class <pre>
    class A {
      public:
      A(int i);
      ~A();
      private:
      B b;   // Want <- this guy to be constructed with i in A's constructor!
    };
</pre>
I want b to be constructed in the constructor with particular parameters that aren't known until A is constructed. If I were to do the following in A's constructor:
<pre>
    A::A(int i) {   
      B b(i);
      // Or even if I try to do b = B::B(i);
    }
</pre>
I notice that b get's allocated twice on the stack! aghghg.

Then I found out that what I can do in A's constructor is:
<pre>
A::A() : b(B::B(7)) {

}</pre>
And b only gets allocated on the stack once!

But this is pretty clunky. Anyone got a better idea? Remember, the constructor should only be called once!

Is this the standard way of allocating objects NON-dynamically with important parameters? What if we can shove b's construction into that fancy argument list thing!? You're forced to either dynamically allocate, or construct TWICE on the stack!

Bonus Question: When does b get deallocated? Is it after or right before A's destructor