English 中文(简体)
提升:由 mu成员组成的帝国
原标题:boost::serialization with mutable members

利用促进作用:帝国化,“最佳”方式是什么,使 mu成员含有附庸和衍生价值的物体序列化?

class Example
{
public:
    Example(float n) : 
        num(n),
        sqrt_num(-1.0)
    {}

    // compute and cache sqrt on first read
    float get_sqrt() const
    { 
        if(sqrt_num < 0) 
            sqrt_num = sqrt(num);
        return sqrt_num;
    }

    template <class Archive> 
    void serialize(Archive& ar, unsigned int version)
    { ... }
private:
    float num;
    mutable float sqrt_num;
};

因此,出于维护原因,我想避免将序列号分离为单独的“()”和(或)装载方法。

序列号的次级实施:

    template <class Archive> 
    void serialize(Archive& ar, unsigned int version)
    {
        ar & num;
        sqrt_num = -1.0;
    }

处理“航空化”案,但在序列化案件中,焦土价值被杀死,必须重新计算。

本案的最佳做法是什么?

最佳回答

页: 1 档案:是有效载荷的 领域,如果是真的,则装载热量值。

template <class Archive> 
void serialize(Archive& ar, unsigned int version)
{
    ar & num;
    if(Archive::is_loading::value == true)
        sqrt_num = -1.0;
}
问题回答

安排你的储蓄和装货方法,并不意味着你必须保留您的序号。 你们可以把他们分开,然后再次与他们一道履行共同的职能。

private:
  friend class boost::serialization::access;

  BOOST_SERIALIZATION_SPLIT_MEMBER()

  template <class Archive>
  void save(Archive& ar, const unsigned int version) const {
      const_cast<Example*>(this)->common_serialize(ar, version);
  }

  template <class Archive>
  void load(Archive& ar, const unsigned int version) {
      common_serialize(ar, version);
      sqrt_num = -1;
  }

  template <class Archive>
  void common_serialize(Archive& ar, const unsigned int version) {
      ar & num;
  }

您可能注意到<代码>const_cast。 这令人感到遗憾。 虽然<条码>星号 成员功能不是储蓄业务的最基本功能,但<条码>save 成员功能必须一致。 只要重新编号的物体是最初宣布的 con,就能够安全投放如上所示。 文件

以上改动后,你的代码将正确印制<代码>ex1和ex2,而且你只得保持一份序列化代码。 www.un.org/Depts/DGACM/index_french.htm 守则仅载有重新启用物体内部切身的专用代码;save功能未触及海滩。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签