<i id='EFega'><tr id='EFega'><dt id='EFega'><q id='EFega'><span id='EFega'><b id='EFega'><form id='EFega'><ins id='EFega'></ins><ul id='EFega'></ul><sub id='EFega'></sub></form><legend id='EFega'></legend><bdo id='EFega'><pre id='EFega'><center id='EFega'></center></pre></bdo></b><th id='EFega'></th></span></q></dt></tr></i><div id='EFega'><tfoot id='EFega'></tfoot><dl id='EFega'><fieldset id='EFega'></fieldset></dl></div>

  • <small id='EFega'></small><noframes id='EFega'>

    <tfoot id='EFega'></tfoot>

      <legend id='EFega'><style id='EFega'><dir id='EFega'><q id='EFega'></q></dir></style></legend>
        <bdo id='EFega'></bdo><ul id='EFega'></ul>
      1. 我可以将 boost::make_shared 与私有构造函数一起使用吗?

        时间:2023-07-19
      2. <tfoot id='cYsuk'></tfoot>

            <bdo id='cYsuk'></bdo><ul id='cYsuk'></ul>
            <i id='cYsuk'><tr id='cYsuk'><dt id='cYsuk'><q id='cYsuk'><span id='cYsuk'><b id='cYsuk'><form id='cYsuk'><ins id='cYsuk'></ins><ul id='cYsuk'></ul><sub id='cYsuk'></sub></form><legend id='cYsuk'></legend><bdo id='cYsuk'><pre id='cYsuk'><center id='cYsuk'></center></pre></bdo></b><th id='cYsuk'></th></span></q></dt></tr></i><div id='cYsuk'><tfoot id='cYsuk'></tfoot><dl id='cYsuk'><fieldset id='cYsuk'></fieldset></dl></div>

                <small id='cYsuk'></small><noframes id='cYsuk'>

                  <tbody id='cYsuk'></tbody>
                <legend id='cYsuk'><style id='cYsuk'><dir id='cYsuk'><q id='cYsuk'></q></dir></style></legend>

                1. 本文介绍了我可以将 boost::make_shared 与私有构造函数一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  考虑以下事项:

                  class DirectoryIterator;
                  
                  namespace detail {
                      class FileDataProxy;
                  
                      class DirectoryIteratorImpl
                      {
                          friend class DirectoryIterator;
                          friend class FileDataProxy;
                  
                          WIN32_FIND_DATAW currentData;
                          HANDLE hFind;
                          std::wstring root;
                  
                          DirectoryIteratorImpl();
                          explicit DirectoryIteratorImpl(const std::wstring& pathSpec);
                          void increment();
                          bool equal(const DirectoryIteratorImpl& other) const;
                      public:
                          ~DirectoryIteratorImpl() {};
                      };
                  
                      class FileDataProxy //Serves as a proxy to the WIN32_FIND_DATA struture inside the iterator.
                      {
                          friend class DirectoryIterator;
                          boost::shared_ptr<DirectoryIteratorImpl> iteratorSource;
                          FileDataProxy(boost::shared_ptr<DirectoryIteratorImpl> parent) : iteratorSource(parent) {};
                      public:
                          std::wstring GetFolderPath() const {
                              return iteratorSource->root;
                          }
                      };
                  }
                  
                  class DirectoryIterator : public boost::iterator_facade<DirectoryIterator, detail::FileDataProxy, std::input_iterator_tag>
                  {
                      friend class boost::iterator_core_access;
                      boost::shared_ptr<detail::DirectoryIteratorImpl> impl;
                      void increment() {
                          impl->increment();
                      };
                      bool equal(const DirectoryIterator& other) const {
                          return impl->equal(*other.impl);
                      };
                      detail::FileDataProxy dereference() const {
                          return detail::FileDataProxy(impl);
                      };
                  public:
                      DirectoryIterator() {
                          impl = boost::make_shared<detail::DirectoryIteratorImpl>();
                      };
                  };
                  

                  看起来DirectoryIterator 应该可以调用boost::make_shared,因为它是DirectoryIteratorImpl 的朋友.但是,此代码无法编译,因为 DirectoryIteratorImpl 的构造函数是私有的.

                  It seems like DirectoryIterator should be able to call boost::make_shared<DirectoryIteratorImpl>, because it is a friend of DirectoryIteratorImpl. However, this code fails to compile because the constructor for DirectoryIteratorImpl is private.

                  由于这个类是一个内部实现细节,DirectoryIterator 的客户端不应该接触,如果我能保持构造函数私有就太好了.

                  Since this class is an internal implementation detail that clients of DirectoryIterator should never touch, it would be nice if I could keep the constructor private.

                  这是我对 make_shared 的基本误解,还是我需要将某种 boost 片段标记为 friend 以便调用编译?

                  Is this my fundamental misunderstanding around make_shared or do I need to mark some sort of boost piece as friend in order for the call to compile?

                  推荐答案

                  您确实需要为此添加一些增强件.基本上 make_shared 正在调用构造函数,并且这是从友元函数内完成的事实对编译器无关紧要.

                  You will indeed need to make some boost pieces friend for this. Basically make_shared is calling the constructor and the fact that this is done from within a friend function does not matter for the compiler.

                  好消息是 make_shared 正在调用构造函数,而不是任何其他部分.所以只要让 make_shared 成为朋友就可以了……但这意味着任何人都可以创建一个 shared_ptr...

                  The good news though is that make_shared is calling the constructor, not any other piece. So just making make_shared friend would work... However it means that anyone could then create a shared_ptr<DirectoryIteratorImpl>...

                  这篇关于我可以将 boost::make_shared 与私有构造函数一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:std::make_pair 与 C++ 11 下一篇:序列化原始 boost::variant 是否安全?

                  相关文章

                    <legend id='JSZ2B'><style id='JSZ2B'><dir id='JSZ2B'><q id='JSZ2B'></q></dir></style></legend>

                    • <bdo id='JSZ2B'></bdo><ul id='JSZ2B'></ul>

                    <small id='JSZ2B'></small><noframes id='JSZ2B'>

                    1. <tfoot id='JSZ2B'></tfoot>

                      <i id='JSZ2B'><tr id='JSZ2B'><dt id='JSZ2B'><q id='JSZ2B'><span id='JSZ2B'><b id='JSZ2B'><form id='JSZ2B'><ins id='JSZ2B'></ins><ul id='JSZ2B'></ul><sub id='JSZ2B'></sub></form><legend id='JSZ2B'></legend><bdo id='JSZ2B'><pre id='JSZ2B'><center id='JSZ2B'></center></pre></bdo></b><th id='JSZ2B'></th></span></q></dt></tr></i><div id='JSZ2B'><tfoot id='JSZ2B'></tfoot><dl id='JSZ2B'><fieldset id='JSZ2B'></fieldset></dl></div>