In the code below, class B has a member that is of type class A (varA1). I want to create a class B object where the member varA1 is intended to use the non-default constructor
Use a member initialization list:
B::B(int v1) : var1(v1), varA1(v1) {
cout << "B int constructor" << endl;
}
Note that members are initialized (constructed) in the same order that they're declared in the class, so switching orders in the member initialization list won't change the order in which construction happens (and hopefully your compiler will warn you of this). This little detail becomes important if you try to construct varA1
from var1
and var1
is declared after varA1
in the class definition.
And by the way, all this line does (inside the B::B(int v1)
constructor):
A varA1(int v1);
is forward declare a function named varA1
that takes an int
parameter and returns an A
object. This is semi-similar to the most vexing parse, though this isn't really a case of the most vexing parse.
You could use an initializer list in your B::B(int) constructor:
B::B(int v1) : varA1(v1) {
cout << "B int constructor" << endl;
var1 = v1;
}
If you do not want a default ctor, you might use
A(void) = delete;
But recognize that you can temporarily add this declaration as a diagnostic measure to get the compiler to inform you for which code it wants to use the default ctor.
By replacing
A() { std::cout << "A default constructor" << std::endl;}
with
A() = delete;
The compiler will complain about the use of deleted function 'A::A()' in the body of B::B(int v1).
Thus, a brief inspection identifies that the second data attribute of B, i.e. "B::varA1", uses the default ctor of A (now deleted).
You also might consider 'fixing' the B ctor to NOT use the default ctor of A by explicitly invoking "varA1 (0)" in the initializer list of B ctor.