Initializing class without default constructor

不想你离开。 提交于 2019-12-10 13:28:36

问题


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:


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

B::B() : a(0, 0)
{
    // ctor here
}


来源:https://stackoverflow.com/questions/3704722/initializing-class-without-default-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!