基类中删除的构造函数会影响子类吗?

时间:2018-12-12 12:54:39

标签: c++ c++11 copy-constructor

我在基类中删除了一个复制构造函数,但是编译器是否会在子类中创建一个隐式的复制构造函数,我不知道吗?还是基类中已删除的构造函数会阻止它?

template <typename val_t>
class exp_t {
public:
    using vals_t = std::vector<val_t>;

    exp_t() {}
    exp_t(const exp_t<val_t> &) = delete;
    exp_t(exp_t &&) = default;
    virtual ~exp_t() {}

    exp_t<val_t> &operator=(const exp_t<val_t> &) = delete;
    exp_t<val_t> &operator=(exp_t<val_t> &) = delete;
    exp_t<val_t> &operator=(exp_t &&) = default;
};


template <typename val_t>
class fact_t: public exp_t<val_t> {
    using vals_t = std::vector<val_t>;

    val_t m_value;
public:
    fact_t(val_t &&value) : m_value{std::forward<val_t>(value)} {}
    fact_t(fact_t &&) = default;
};

fact_t是否具有隐式副本构造函数?(GCC 7)

1 个答案:

答案 0 :(得分:3)

否,因为默认的副本构造函数将调用父级的副本构造函数(已删除),所以将无法正常工作。

为什么不简单地进行测试:

int main() {
    auto x = fact_t<int>(5);
    auto y = x;
}

结果:

copytest.cpp: In function 'int main()':
copytest.cpp:32:14: error: use of deleted function 'fact_t<int>::fact_t(const fact_t<int>&)'
     auto y = x;
              ^
copytest.cpp:21:7: note: 'fact_t<int>::fact_t(const fact_t<int>&)' is implicitly declared as deleted because 'fact_t<int>' declares a move constructor or move assignment operator
 class fact_t: public exp_t<val_t> {
       ^~~~~~