1

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?

2 Answers 2

7

The problem with

Derive(Derive&& p) xxx { *this = std::move(p); }

is that in the xxx part you have an empty member initialization list. That means the compiler will insert one for you for the base class since all members get initialized in the member initialization list before the constructor body is executed. That compiler generated version would look like

Derive(Derive&& p) : Base() { *this = std::move(p); }

And you can't do Base() because it's deleted. What you want is

Derive(Derive&& p) : Base(std::move(p)) {}

or even shorter

Derive(Derive&& p) = default;
2

You don't need to explicitly delete the default ctor in your base class. Simply defining a ctor that requires a parameter prevents the compiler from generating a default ctor, so your base class can be just:

struct Base {
    Base(int a_) : a{a_} {};
private:
  int a; //new mandatory param;
};

Likewise, in your derived class, defining a ctor that takes a parameter prevents the compiler from generating a default ctor for it either. At least so far, nothing you've mentioned seems to indicate you need to explicitly define any of the special member functions for your derived class either, so it can become simply:

struct Derive: public Base
{
    Derive(int a_) : Base(a_) {};
};

...and now the code compiles just fine, and any attempt at creating an instance of either Base or Derive without specifying a parameter to the ctor will fail (won't compile).

As an aside, since you're using it as a base class, you probably want Base to make its dtor virtual. Here it's probably appropriate to define it as defaulted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.