C++ Default Constructor Called

前端 未结 3 898
醉酒成梦
醉酒成梦 2021-01-29 01:36

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

3条回答
  •  [愿得一人]
    2021-01-29 02:10

    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; 
    }
    

提交回复
热议问题