Denis Steinman
Denis Steinman

Reputation: 7799

Does a deleted constructor in base class influences on child class?

I deleted a copy constructor in base class but I can't get if the compiler will create an implicit copy constructor in child classes? Or does a deleted constructor in the base class prevent it?

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;
};

Will fact_t have an implicit copy constructor? (GCC 7)

Upvotes: 6

Views: 1928

Answers (1)

mrks
mrks

Reputation: 8333

No, as the default copy constructor would call the parent's copy constructor (which is deleted), this won't work.

Why didn't you simply test it:

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

Result:

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> {
       ^~~~~~

Upvotes: 6

Related Questions