Does the default constructor work without a move constructor?

If the class does not have a default constructor, since it should always initialize internal variables, will it follow that it should not have a move constructor?

class Example final {
public:
  explicit Example(const std::string& string) : string_(
    string.empty() ? throw std::invalid_argument("string is empty") : string) {}
  Example(const Example& other) : string_(other.string_) {}
private:
  Example() = delete;
  Example(Example&& other) = delete;
  Example& operator=(const Example& rhs) = delete;
  Example& operator=(Example&& rhs) = delete;
  const std::string string_;
};

This class always expects the inner row to be specified by a non-empty string, and the inner row to be copied between objects Example. I will correct that the move constructor is not applied here, because if the example were moved, would it have to leave the string empty with a call std::move?

+5
source share
1 answer

If the class does not have a default constructor, since it should always initialize internal variables, will it follow that it should not have a move constructor?

No, I would not say that .

Example , , .

, , Example. , - Example (.. string_ ), .

, Example , string_ - ? - string_?.

Example ​​, , , , - , string_.

, .

+6

All Articles