对子类使用基本默认构造函数(带参数) [英] Using base default constructor (with parameters) for child class
本文介绍了对子类使用基本默认构造函数(带参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类A,它必须有一个类传递给它;从A我有两个类B和C; B和C可以使用A中的构造函数作为默认构造函数。
I have a class A which has to have a a class passed to it; From A I have two classes B and C; is it possible for B and C to use the constructor from A, as apposed to the default constructor.
A
/ \
B C
A::A(randomNumber &rnd)
{
....
}
推荐答案
是的,可以:
class B
{
public:
B(randomNumber& rnd) : A(rnd) { }
// ...
};
如果您想呼叫 A
构造函数在 B
的默认构造函数中,您将必须传递一个全局对象: A
的结构接受lvalue引用,创建临时不是一个选项。
If you want to call A
's constructor in B
's default constructor, you will have to pass a global object: since A
's construct accepts an lvalue reference, creating a temporary is not an option.
B() : A(global_random_number);
这篇关于对子类使用基本默认构造函数(带参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文