C++ 序列化指针本身而不是目标对象

C++ 序列化指针本身而不是目标对象,c++,boost,boost-serialization,C++,Boost,Boost Serialization,我有一个使用boost序列化程序保存和加载的数据类 该类包括两个成员En*发送者,En*接收者,它们是我的系统中已创建的两个对象的成员。 我不需要再次创建它们。我只需要发送(序列化)他们的地址,作为另一端的参考 如果我使用普通的指针,如En*sender,En*receiver,boost将序列化整个对象(我不需要) 所以我想我应该使用产生错误的En**sender,En**receiver。我可以知道我应该如何修改课程以达到我的目的吗 非常感谢 class dataMessage {

我有一个使用boost序列化程序保存和加载的数据类

该类包括两个成员
En*发送者
En*接收者
,它们是我的系统中已创建的两个对象的成员。 我不需要再次创建它们。我只需要发送(序列化)他们的
地址
,作为另一端的参考

如果我使用普通的指针,如
En*sender
En*receiver
,boost将序列化整个对象(我不需要)

所以我想我应该使用产生错误的
En**sender
En**receiver
。我可以知道我应该如何修改课程以达到我的目的吗

非常感谢

class dataMessage
{

    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive &ar, const unsigned int version)
    {            
        ar & sender;
        ar & receiver;
    }
    //I figured I should user pointer to pointer coz in case of the normal pointer,
    //the serilizer would serializes the object that the pointer is pointing to;
    //whereas I just need to save the 'pointer' to object so that we
    //can use it as a reference at the other end.
    En **sender;
    En **receiver;
public:
    dataMessage(){
        sender = receiver = 0;
    }
    void setDataClassType();
    virtual void registerType(boost::archive::text_oarchive &oa)
    {
        oa.register_type(static_cast<dataMessage *>(NULL));
    }

    virtual void registerType(boost::archive::text_iarchive &ia)
    {
        ia.register_type(static_cast<dataMessage *>(NULL));
    }
};

似乎只有手动存储地址,否则boostserializer将序列化目标对象。因此,我将发送方-接收方类型修改为:

class dataMessage
{

 //......
public:
//.....
    //a proper size of integer is used coz in case of the normal pointer,
    //the serilizer serializes the object that the pointer is pointing to
    //whereas we just need to save the 'pointer' to object so that we
    //can use it as a reference at the other end. so instead of storing 'sim_mob::Entity *'
    //we store:
#if __x86_64__  //64 bit
    unsigned long sender;
    unsigned long receiver;
#else          //32 bit
    unsigned int sender;
    unsigned int receiver;

#endif


    dataMessage(){
        sender = receiver = 0;
    }
//...
};

稍后使用指针时,我将对适当的指针类型进行简单转换。

什么是“另一端”?指针仅在您的程序中有效。您好,是的,另一端是我的程序。(故事:在我们的模拟器中,我使用序列化在代理之间发送/接收数据。这种发送/接收必须遵循无线网络不确定性的模型,如延迟、丢失等。因此,我序列化数据包,将发送方-接收方信息放入数据包中,然后通过写入文件[send file]将数据包发送到另一个模拟器。第二个模拟器读取此文件,而不关心数据包的内容。它只是在延迟后将内容写入另一个文件[接收文件](或者根本不发送,类似于“丢失”).第一个模拟器读取…读取接收文件,反序列化并根据接收器的信息,将信息传递到接收器的传入缓冲区)。在这里,我只需要接收器信息就是接收对象的地址…其余的都很清楚(我想)如果所有这些都发生在同一个程序/进程中,为什么要序列化任何内容?您可以只传递一个指向顶级数据结构的指针。无论如何,您可以将指针强制转换为某个大小合适的整数类型,然后对其进行序列化,并在反序列化时向另一个方向强制转换。我认为您需要单独的序列化和反序列化方法,教程中有一个例子说明了如何做到这一点。@n.m.是的,我认为转换为整数类型是唯一的方法。谢谢。并且,根据教程,我还将发送和接收分开。最后是关于“传递指向顶级数据结构的指针”…我没有使用堆。如果你问为什么,我没有答案。只是不想动态创建数据对象并在之后清理它们。因此,数据是在本地生成的,并以与前面解释的相同的方式还原。
class dataMessage
{

 //......
public:
//.....
    //a proper size of integer is used coz in case of the normal pointer,
    //the serilizer serializes the object that the pointer is pointing to
    //whereas we just need to save the 'pointer' to object so that we
    //can use it as a reference at the other end. so instead of storing 'sim_mob::Entity *'
    //we store:
#if __x86_64__  //64 bit
    unsigned long sender;
    unsigned long receiver;
#else          //32 bit
    unsigned int sender;
    unsigned int receiver;

#endif


    dataMessage(){
        sender = receiver = 0;
    }
//...
};