mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-17 04:44:34 +03:00
Update CppCoreGuidelines.md (#1851)
This commit is contained in:
@@ -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).
|
||||||
|
|||||||
Reference in New Issue
Block a user