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

  • <legend id='es5uu'><style id='es5uu'><dir id='es5uu'><q id='es5uu'></q></dir></style></legend>

    <tfoot id='es5uu'></tfoot>
      <bdo id='es5uu'></bdo><ul id='es5uu'></ul>

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

        获取非侵入式 boost 序列化 C++ 的私有数据成员

        Get private data members for non intrusive boost serialization C++(获取非侵入式 boost 序列化 C++ 的私有数据成员)

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

                <bdo id='yQi4A'></bdo><ul id='yQi4A'></ul>
                <legend id='yQi4A'><style id='yQi4A'><dir id='yQi4A'><q id='yQi4A'></q></dir></style></legend>
              • <small id='yQi4A'></small><noframes id='yQi4A'>

                  <tbody id='yQi4A'></tbody>
              • <i id='yQi4A'><tr id='yQi4A'><dt id='yQi4A'><q id='yQi4A'><span id='yQi4A'><b id='yQi4A'><form id='yQi4A'><ins id='yQi4A'></ins><ul id='yQi4A'></ul><sub id='yQi4A'></sub></form><legend id='yQi4A'></legend><bdo id='yQi4A'><pre id='yQi4A'><center id='yQi4A'></center></pre></bdo></b><th id='yQi4A'></th></span></q></dt></tr></i><div id='yQi4A'><tfoot id='yQi4A'></tfoot><dl id='yQi4A'><fieldset id='yQi4A'></fieldset></dl></div>
                  本文介绍了获取非侵入式 boost 序列化 C++ 的私有数据成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我尝试为我的非成员 serialize() 函数提供 A 类的 getter,因为从成员访问是私有的.

                  I have tried providing getters of class A for my non-member serialize() function` since accessing from members is private.

                  template<typename T>
                  class A
                  {
                  public:
                    A(const T& id) : m_id(id) {}
                    T& getRef() { return m_id; } // not giving good results
                    T  getId()  { return m_id; } // not giving good results
                    const T& getRef() const { return m_id; } // not giving good results
                  private: // I would like to keep it private
                    T m_id;
                  }
                  
                  namespace boost { namespace serialization {
                  
                  template<class Archive,typename T>
                  void serialize(Archive &ar, A &a, const unsigned int version)
                  {
                      // ar &BOOST_SERIALIZATION_NVP(a.m_id); // I would like to avoid that it works if m_id is public
                      ar &BOOST_SERIALIZATION_NVP(a.GetRef()); // I want this !
                  }
                  
                  }}
                  
                  // and later I use
                  std::ofstream ofs("test.xml");
                  boost::archive::xml_oarchive oa(ofs);
                  A<int> a(42);
                  oa << BOOST_SERIALIZATION_NVP(a);
                  

                  不幸的是,当我尝试使用 getter GetRef()GetId().
                  如果我在公开时直接访问 m_id,则效果很好.

                  Unfortunately the execution keeps telling me uncaught exception of type boost::archive::xml_archive_exception - Invalid XML tag name when I try to use getters either GetRef()or GetId().
                  It works well if I access directly to m_id when it is public.

                  有什么好的方法吗?

                  推荐答案

                  1. 你可以使用老派的好朋友:

                  1. You can use good old-fashioned friends:

                  生活在 Coliru

                  template <typename T>
                  class A {
                    public:
                      A(const T &id) : m_id(id) {}
                    private:
                      template <typename Ar, typename U> friend void boost::serialization::serialize(Ar&,A<U>&,const unsigned);
                      T m_id;
                  };
                  
                  namespace boost {
                  namespace serialization {
                      template <class Archive, typename T>
                      void serialize(Archive &ar, A<T> &a, const unsigned int)
                      {
                          ar & BOOST_SERIALIZATION_NVP(a.m_id);
                      }
                  }
                  }
                  


                • 您可以使用 getRef() 方法.这个

                  • 不需要朋友(较少打扰)
                  • 需要 make_nvp(因为你不能使用 a.getRef() 作为 XML 元素名称
                  • requires no friends (less intrusive)
                  • requires make_nvp (because you can't use a.getRef() as an XML element name

                  遗憾的是,引用 getter 以一种可怕的方式破坏了封装.我个人更喜欢首先公开 m_id.

                  Sadly, having the reference getter break encapsulation in a horrific way. I'd personally prefer to have m_id public in the first place, instead.

                  生活在 Coliru

                  template <typename T>
                  class A {
                  public:
                      A(const T &id) : m_id(id) {}
                  
                      T& getRef()             { return m_id; } 
                      T const& getRef() const { return m_id; } 
                  private:
                      T m_id;
                  };
                  
                  namespace boost {
                  namespace serialization {
                      template <class Archive, typename T>
                      void serialize(Archive &ar, A<T> &a, const unsigned int)
                      {
                          ar & boost::serialization::make_nvp("m_id", a.getRef());
                      }
                  }
                  }
                  

                  奖励积分:

                • 您可以使用pimpl"风格的结构.您可以在 A<> 中转发声明一个结构体:

                  Bonus points:

                • You can use a 'pimpl' style struct. You can forward declare a struct inside A<>:

                  template <typename T>
                  class A {
                  public:
                      struct access;
                  
                      A(const T &id) : m_id(id) {}
                  private:
                      T m_id;
                  };
                  

                  这比 getRef() 方法的侵入性要小,后者只是完全破坏了封装.现在,您可以在该类中隐藏私有访问权限:

                  That's less intrusive than the getRef() approach which simply breaks encapsulation all the way. Now, you can hide the private access inside this class:

                  namespace boost {
                  namespace serialization {
                      template <class Archive, typename T>
                      void serialize(Archive &ar, A<T> &a, const unsigned int version)
                      {
                          A<T>::access::serialize(ar, a, version);
                      }
                  }
                  }
                  

                  当然,您仍然需要实现它,但这可以在单独的标题中完成,并且根本不会影响类 A<>(或其任何特化):

                  Of course you still need to implement it, but this can be done in a separate header and doesn't influence class A<> (or any of its specializations) at all:

                  template <typename T>
                  struct A<T>::access {
                      template <class Archive>
                      static void serialize(Archive &ar, A<T> &a, const unsigned int) {
                          ar & BOOST_SERIALIZATION_NVP(a.m_id);
                      }
                  };
                  

                  看看它生活在 Coliru 以及p>

                • 这篇关于获取非侵入式 boost 序列化 C++ 的私有数据成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)
                    <bdo id='BdOoG'></bdo><ul id='BdOoG'></ul>
                      <legend id='BdOoG'><style id='BdOoG'><dir id='BdOoG'><q id='BdOoG'></q></dir></style></legend>
                    1. <tfoot id='BdOoG'></tfoot>

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

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