let's say i have a class A and a class B. B is used as a member in A. B does not have a default constructor but one that requires a parameter.
class B {
B(int i) {}
};
class A {
B m_B;
A()
{
m_B(17); //this gives an error
}
};
how can i still use B as a member in A?
Use initialization list.
class B {
public:
B(int i) {}
};
class A {
B m_B;
public:
A() : m_B(17) {}
};
BTW, to reset m_B
somewhere outside of the constructor, the correct syntax is:
m_B = B(17);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With