Initializing class without default constructor

2019-06-19 02:05发布

If I have a class A with only a copy constructor and a constructor with parameters int and int, and I place that class inside a class B:

class B
{
public:
    B();
private
    A a;
}

How would I initialize a inside B's constructor?

I've tried a(0, 0), a = A(0, 0), but not surprisingly neither worked, and I receive a

error: no matching function for call to ‘A::A()’

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-06-19 02:32

In B's constructor, you would do something like this:

B::B() : a(0, 0)
{
    // ctor here
}
查看更多
登录 后发表回答