初始化没有默认构造函数的类 [英] Initializing class without default constructor
本文介绍了初始化没有默认构造函数的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一个类 A
只有一个复制构造函数和参数 int
和 int
,我把这个类放在类中 B
:
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;
}
如何初始化 a $ c $
How would I initialize a
inside B's constructor?
我尝试了 a(0,0)
, a = A(0,0)
,但并不奇怪,两者都不起作用,我收到一个
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()’
推荐答案
在B的构造函数中,你可以这样做:
In B's constructor, you would do something like this:
B::B() : a(0, 0)
{
// ctor here
}
这篇关于初始化没有默认构造函数的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文