Construction of Member Objects

Let's say my program has 2 classes, CInner and COuter. One of COuter's members is an object of type CInner.
Fact: Creating an object of type COuter runs the constructor-without-parameters of CInner. Destroying an object of type COuter runs the destructor-without-parameters of CInner.
Question: Is it possible to make the line COuter obj; run CInner's constructor-with-parameters for obj's CInner member? Or am I limited to the CInner(void) constructor (unless the member is a pointer assigned dynamic memory, but tgis is not the case)?
Creating an object of type COuter runs the constructor-without-parameters of CInner


This is not a fact.

You specify which constructor of CInner you want to use in COuter's initializer list, and the parameters you want to call it with.

If you fail to do so, then the default (no parameters) is used by default.

Destroying an object of type COuter runs the destructor-without-parameters of CInner.


This is true. Destructors never have parameters.


To answer your question: it's all about initializer lists:

1
2
3
4
5
6
7
8
9
10
11
class COuter
{
private:
  CInner inner;

public:
  COuter()   // some ctor
    : inner(1,2,3)  // initializer list -- call CInner's ctor
  {
  }
};
I see...thanks :)
Topic archived. No new replies allowed.