tags:

views:

61

answers:

1

hello, 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?

+10  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);
KennyTM