I have an existing piece of code like this:
struct Base { Base() = default; }; struct Derive: public Base { Derive() = default; Derive(const Derive&) = delete; Derive(Derive&& p) { *this = std::move(p); } Derive& operator = (const Derive& p) = delete; Derive& operator = (Derive&& p) { return *this; } }; int main() { Derive p; } It compiles and works. Now I want to change class definition a little bit so that Base or Derived class is always constructed with certain integer parameter and never constructed without such param.
So if I try the following changes:
struct Base { Base() = delete; Base(int a_) : a{a_} {}; private: int a; //new mandatory param; }; struct Derive: public Base { Derive() = delete; Derive(int a_) : Base(a_) {}; Derive(const Derive&) = delete; Derive(Derive&& p) { *this = std::move(p); } Derive& operator = (const Derive& p) = delete; Derive& operator = (Derive&& p) { return *this; } }; int main() { Derive p{1}; } I get the compilation error
main.cpp:15:2: error: call to deleted constructor of 'Base' Derive(Derive&& p) { *this = std::move(p); } ^ main.cpp:4:2: note: 'Base' has been explicitly marked deleted here Base() = delete; ^ 1 error generated. Apparently this way doesn't work. So how do I modify the code so it compiles and no param constructor is never called without error?
https://stackoverflow.com/questions/65571811/how-to-use-move-constructor-with-deleted-default-consturctor January 05, 2021 at 08:49AM
没有评论:
发表评论