c++ - 两个成员,默认在 Base 中初始化,可能在 Derived 中非默认初始化
问题描述
我有一个带有默认构造函数的基类。
class Base
{
public:
Base();
private:
Type1 m_ofType1;
Type2 m_ofType2;
}
Base::Base()
: m_ofType1(defaultExpr1)
, m_ofType2(defaultExpr2)
{
}
我有一个派生类,可能是:
- 默认构造,m_ofType1 和 m_ofType2 由它们各自的默认表达式初始化,
- 仅提供 Type1,或
- 仅提供 Type2。
class Derived : public base
{
public:
Derived(); // m_ofType1 and m_ofType2 will be initialized with their default expressions.
Derived(Type1 overrideOfType1); // m_ofType1 will be initialized with overrideOfType1 and m_ofType2 will be initialized with its default expression.
Derived(Type2 overrideOfType2); // m_ofType1 will be initialized with its default expression and m_ofType2 will be initialized with overrideOfType2.
}
Base 用于生产代码,Derived 用于测试代码。我想实现没有数据或代码重复的构造函数,但我不知道如何。
鉴于约束,我希望已经说清楚了,你们都知道如何在没有数据或代码重复的情况下实现上述构造函数吗?
代码重复示例:
我们可以将受保护的构造函数添加到基础:
Base(Type1);
Base(Type2);
这意味着 Derived 构造函数只会将调用转发给它们各自的 Base 构造函数。
Derrived::Derrived()
: Base()
{
}
Derived::Derived(Type1 overrideOfType1)
: Base(overrideOfType1)
{
}
Derived::Derived(Type1 overrideOfType2)
: Base(overrideOfType2)
{
}
在这样做时,问题发生了一些变化。如何在没有数据或代码重复的情况下实现 Base 构造函数。
这是我最好的尝试。它不起作用,我会告诉你为什么。
首先,向 Base 添加另一个构造函数:
Base(Type1 ofType1, Type2 ofType2);
Base::Base() : Base(defaultExpr1, defaultExpr2) { }
Base::Base(Type1 overrideOfType1) : Base(overrideOfType1, defaultExpr2) { }
Base::Base(Type2 overrideOfType2) : Base(defaultExpr1, overrideOfType2) { }
Base::Base(Type1 ofType1, Type1 ofType2) : m_ofType1(ofType1) , m_ofType2(ofType2) { }
You can see that defaultExpr1 and defaultExpr2 have been duplicated.
解决方案
我认为这Derived
几乎是一个红鲱鱼。基本上,您要求 3 种方法来构造 aBase
并做到这一点,它Base
必须提供适当的构造函数,类似于:
struct Base {
Type1 m_ofType1{defaultExpr1};
Type2 m_ofType2{defaultExpr2};
Base() = default;
Base(Type1 x) : m_ofType1(x) {}
Base(Type2 x) : m_ofType2(x) {}
};
默认初始值设定项 (defaultExpr1
和defaultExpr2
) 可以被构造函数初始值设定项列表中的初始值设定项覆盖。
现在根据需要添加访问限制和调用这些构造函数的派生类(也许将最后两个受保护的和数据成员设为私有)。继续你的例子,那将是
struct Derived : Base {
Derived(Type1 x) : Base(x) {}
Derived(Type2 x) : Base(x) {}
};
我怀疑你能得到比这更短的时间。
推荐阅读
- python-3.x - 使用 GridSearchCV 进行网格搜索 - 使用 ImageDataGenerator(keras)的 scikit-learn(超参数调整)?
- c# - Angular 应用程序从后端获取大量数据,并且它们在 html 表中显示不佳
- c# - 转换 long.MaxValue -> float -> long 会导致值溢出
- java - 如何使用 Hibernate / JPA 将 Oracle 类型对象转换为 Java 对象?
- node.js - 使用 puppeteer 初始化浏览器后即时更改语言环境
- perforce - 如何在没有差异的情况下列出 perforce 更改列表中的文件?
- kubernetes - 我们是否需要在 Kubernetes 节点之间打开所有端口
- python - 使用 matplotlib 修改 scipy stats.probplot 绘图功能
- javascript - React + Redux:未处理的拒绝(TypeError):传播不可迭代实例的无效尝试
- python - 如何有效地过滤每行矩阵的最大元素