Watch the following example:
class A {
public:
A(int param1, int param2, int param3) {
// ...
}
};
class B : public A {
public:
B() : m_param1(1), m_param(2), m_param(3), A(m_param1, m_param2, m_param3) {
// ...
}
};
B b;
Obviously, when "b" will be created, A's ctor will be called before the parameters of B will be initialized.
This rule prevents me from creating "wrapper" classes which simplify the class's initialization.
What is the "right way" for doing it?
Thanks, Amir
PS: In my particular case, the parameters are not primitives, this example just helped me to explain myself.
"The parameters are not primitives". So you have something like this?
And you want to pass the members of
B
to the constructor ofA
. How about this?Just make sure
B_params
comes beforeA
in the list ofB
's inherited classes.Just call A's constructor:
EDIT:
Just read your comment to your original post. It's important to be clear about these things :) Anyway, this answer still holds true if you want to create new data in B, track it in B, and pass it to A:
Not sure I'm getting your question.
If you just want something that helps you to initialize A with some given parameters, you should use an A constructor with default values: