Does a deleted constructor in base class influences on child class?
Asked Answered
B

1

6

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)

Barnie answered 12/12, 2018 at 12:54 Comment(0)
F
6

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> {
       ^~~~~~
Fanni answered 12/12, 2018 at 12:55 Comment(2)
Well, if you want to test whether or not there's a copy constructor, create the object, and the create a 2nd object as a copy of the first one. Seems logical to me.Tyson
See an example above on how to test itFanni

© 2022 - 2024 — McMap. All rights reserved.