c++ - 将子类复制到父类,而父类有其他构造函数
问题描述
我在复制struct a
到struct b
. 如果我删除我自己制作的构造函数,它会编译。我缺少什么构造函数来复制构造?
#include <array>
using f_t = float;
struct a {
f_t x;
a() : x(0) {}
};
struct b : public a, public std::array<f_t, 2> {
b() {}
b(const a& a_) : a(a_) {}
b(a&& a_) : a(std::move(a_)) {}
template <typename ...T>
b(const T&... list) : std::array<f_t, 2>{list...} {}
template <typename ...T>
b(T&&... list) : std::array<f_t, 2>{(f_t)std::move(list)...} {}
};
int main() {
a x;
b y = x;
return 0;
}
解决方案
我通过使用解决了这个问题concept
#include <array>
using f_t = float;
struct a {
f_t x;
a() : x(0) {}
};
template <typename T>
concept a_t = std::is_same<T, f_t>::value;
struct b : public a, public std::array<f_t, 2> {
b() {}
b(const a& a_) : a(a_) {}
b(a&& a_) : a(std::move(a_)) {}
template <a_t ...T>
b(const T&... list) : std::array<f_t, 2>{list...} {}
template <a_t ...T>
b(T&&... list) : std::array<f_t, 2>{(f_t)std::move(list)...} {}
};
int main() {
a x;
b y = x;
return 0;
}
推荐阅读
- java - 受保护的访问说明符在 Eclipse 的子类中产生意外的编译时错误
- netlogo - Netlogo:询问“最大数量”
- javascript - javascript for循环执行顺序
- angular - 运行包含 Selenium 代码的测试时量角器出现意外控制台错误
- angular - 在本机脚本中找不到名称 com
- php - 如何根据组合框中的选择将值插入到添加到不同列的数据库表中
- c# - 如何在 Outlook 任务窗格 VSTO 中显示标签
- android - 适用于 android 的 Google ML kit Barcode Scanner 示例应用程序无法检测到条形码
- android - 显示消息后如何取消EditText的文本更改?
- javascript - Ionic 2 可重用的页面组件