diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 240dff4..3778ef1 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -7480,17 +7480,24 @@ Copying a polymorphic class is discouraged due to the slicing problem, see [C.67 class B { public: - virtual owner clone() = 0; B() = default; virtual ~B() = default; - B(const B&) = delete; - B& operator=(const B&) = delete; + virtual gsl::owner clone() const = 0; + protected: + B(const B&) = default; + B& operator=(const B&) = default; + B(B&&) = default; + B& operator=(B&&) = default; + // ... }; - class D : public B { + class D final : public B { public: - owner clone() override; - ~D() override; + ~D() override {}; + gsl::owner clone() const override + { + return new D{*this}; + }; }; Generally, it is recommended to use smart pointers to represent ownership (see [R.20](#Rr-owner)). However, because of language rules, the covariant return type cannot be a smart pointer: `D::clone` can't return a `unique_ptr` while `B::clone` returns `unique_ptr`. Therefore, you either need to consistently return `unique_ptr` in all overrides, or use `owner<>` utility from the [Guidelines Support Library](#SS-views).