gpt4 book ai didi

c++ - 从工厂函数返回 std::unique_ptr 创建纯虚拟接口(interface)的完全隐藏实现

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:18:26 25 4
gpt4 key购买 nike

我正在阅读 Smart Pointer Programming Techniques在 boost 文档中提供。

在“using abstract classes for implementation hiding”部分,他们提供了一个很好的习惯用法来完全隐藏纯虚拟接口(interface)背后的实现。例如:

// Foo.hpp

#include <memory>

class Foo {
public:
virtual void Execute() const = 0;
protected:
~Foo() = default;
};

std::shared_ptr<const Foo> MakeFoo();

// Foo.cpp

#include "Foo.hpp"
#include <iostream>

class FooImp final
: public Foo {
public:
FooImp() = default;
FooImp(const FooImp&) = delete;
FooImp& operator=(const FooImp&) = delete;
void Execute() const override {
std::cout << "Foo::Execute()" << std::endl;
}
};

std::shared_ptr<const Foo> MakeFoo() {
return std::make_shared<const FooImp>();
}

关于 class Foo 中 protected 非虚拟析构函数,文档指出:

Note the protected and nonvirtual destructor in the example above. The client code cannot, and does not need to, delete a pointer to X; the shared_ptr<X> instance returned from createX will correctly call ~X_impl.

我相信我理解。

现在,在我看来,如果工厂函数返回 std::unique_ptr<Foo>,这个漂亮的习惯用法可以用来生成类似单例的实体。 ;用户将被迫move指针,编译时保证不存在拷贝。

但是,唉,除非我更改 ~Foo() = default,否则我无法使代码工作来自 protectedpublic ,我不明白为什么。

换句话说,这是行不通的:

std::unique_ptr<const Foo> MakeUniqueFoo() {
return std::make_unique<const FooImp>();
}

我的问题:

  1. 你能解释一下为什么我需要制作public吗? ~Foo() = default
  2. 只删除 protected 会很危险吗? ?
  3. 单例类的想法值得吗?

最佳答案

  1. 问题与删除器在智能指针中的工作方式有关。

    shared_ptr ,删除器是动态的。当你有 std::make_shared<const FooImp>(); ,该对象中的删除器将调用 ~FooImpl()直接:

    The object is destroyed using delete-expression or a custom deleter that is supplied to shared_ptr during construction.

    该删除器将被复制到 shared_ptr<const Foo> 上创建时。

    unique_ptr ,删除器是类型的一部分。这是:

    template<
    class T,
    class Deleter = std::default_delete<T>
    > class unique_ptr;

    所以当你有 unique_ptr<const Foo> ,这将调用 ~Foo()直接 - 这是不可能的,因为 ~Foo()protected .这就是为什么当你制作 Foo()公开,它有效。工程,如编译。你必须做到 virtual太 - 否则你会通过仅破坏 Foo 来获得未定义的行为的一部分 FooImpl .

  2. 这并不危险。除非您忘记将析构函数设为虚拟,否则需要重复,这将导致未定义的行为。

  3. 这并不是真正的单例。至于值不值?主要基于意见。

关于c++ - 从工厂函数返回 std::unique_ptr<T> 创建纯虚拟接口(interface)的完全隐藏实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31300483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com