Update CppCoreGuidelines.md (#1851)

This commit is contained in:
bgloyer
2021-11-11 13:11:36 -08:00
committed by GitHub
parent 2ba69f786e
commit 86a42c29fc

View File

@@ -7480,17 +7480,24 @@ Copying a polymorphic class is discouraged due to the slicing problem, see [C.67
class B { class B {
public: public:
virtual owner<B*> clone() = 0;
B() = default; B() = default;
virtual ~B() = default; virtual ~B() = default;
B(const B&) = delete; virtual gsl::owner<B*> clone() const = 0;
B& operator=(const B&) = delete; 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: public:
owner<D*> clone() override; ~D() override {};
~D() override; gsl::owner<D*> 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<D>` while `B::clone` returns `unique_ptr<B>`. Therefore, you either need to consistently return `unique_ptr<B>` in all overrides, or use `owner<>` utility from the [Guidelines Support Library](#SS-views). 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<D>` while `B::clone` returns `unique_ptr<B>`. Therefore, you either need to consistently return `unique_ptr<B>` in all overrides, or use `owner<>` utility from the [Guidelines Support Library](#SS-views).