How can I achieve this behavior?
class A
{
int x;
public:
A(int x) : x(x) {}
};
class B
{
A a; // here is the problem
public:
B() : a(1) {} // solution i found
};
int main(void)
{
B b;
return 0;
}
I’m wondering if there is another way of making this work except the answer I found.
No. That’s the proper solution. You explicitly state you don’t want it to be possible to initialize
Awith no parameters, so this outcome shouldn’t surprise you.Or provide a default for
x:making it a default constructor.