gpt4 book ai didi

c++ - 获取非侵入式 boost 序列化 C++ 的私有(private)数据成员

转载 作者:可可西里 更新时间:2023-11-01 17:31:10 31 4
gpt4 key购买 nike

我已经尝试为我的非成员 serialize() 函数提供类 A 的 getter,因为从成员访问是私有(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);

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

有什么好的方法吗?

最佳答案

  1. 你可以使用老式的好 friend :

    Live On 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);
    }
    }
    }

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

    • 不需要 friend (较少打扰)
    • 需要make_nvp (因为您不能使用 a.getRef() 作为 XML 元素名称

    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.

    Live On 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());
    }
    }
    }

    奖励积分:

  3. 您可以使用“pimpl”样式结构。您可以在 A<> 中转发声明一个结构:

    template <typename T>
    class A {
    public:
    struct access;

    A(const T &id) : m_id(id) {}
    private:
    T m_id;
    };

    这比 getRef() 的侵入性更小这种方法完全破坏了封装。现在,您可以在此类中隐藏私有(private)访问权限:

    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);
    }
    }
    }

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

    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);
    }
    };

    查看 Live On Coliru 还有

关于c++ - 获取非侵入式 boost 序列化 C++ 的私有(private)数据成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30594917/

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