jjzjj

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

coder 2024-02-15 原文

我正在阅读 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/

有关c++ - 从工厂函数返回 std::unique_ptr<T> 创建纯虚拟接口(interface)的完全隐藏实现的更多相关文章

  1. ruby-on-rails - Railstutorial : db:populate vs. 工厂女孩 - 2

    在railstutorial中,作者为什么选择使用这个(代码list10.25):http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-usersnamespace:dbdodesc"Filldatabasewithsampledata"task:populate=>:environmentdoRake::Task['db:reset'].invokeUser.create!(:name=>"ExampleUser",:email=>"example@railstutorial.org",:passwo

  2. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  3. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  4. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  5. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  6. ruby-on-rails - 在 ruby​​ 中使用 gsub 函数替换单词 - 2

    我正在尝试用ruby​​中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了

  7. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  8. ruby-on-rails - Nokogiri:使用 XPath 搜索 <div> - 2

    我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll

  9. ruby - 在 Ruby 中按名称传递函数 - 2

    如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只

  10. ruby - 使用 `+=` 和 `send` 方法 - 2

    如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:

随机推荐