使用共享指针和模板增强系列化 [英] Boost serilaization with shared pointer and templates
本文介绍了使用共享指针和模板增强系列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是C ++的新手,我如何序列化具有共享指针和模板的结构。下面是示例代码。
I'm new to C++ and how do i serialize the struct having shared pointer and template . Below is sample code.
#pragma once
#include
#include <boost\serialization\string.hpp>
#include <boost\serialization\shared_ptr.hpp>
//Mydata.hpp file
namespace mydata
{
struct MyData
{
std::string name;
std::string type;
std::shared_ptr<myinfo> myref;
private:
friend class boost::serialization::access;
template<class archive="">
void serialize(Archive &ar, const unsigned int vs)
{
ar & name;
ar & type;
ar & myref;
}
}
}
现在我如何在相应的Mydata.cpp文件中实现共享指针?
now how do i implement in the corresponding Mydata.cpp file for shared pointer ?
推荐答案
您是否正在寻找此 http://www.boost.org/doc/libs/1_55_0/libs/serialization/doc/index.html [ ^ ]?
未尝试过我自己,所以我无法提供超出本文内容的帮助。但是,一句警告:似乎文章引用boost :: shared_ptr
,而不是std :: shared_ptr
!两者可能不一样!
此外文章还指出了存档参考计数器 - 如果您的目标是保存程序的内部状态,例如,这可能很有用。 G。用于调试目的。但是我认为如果你只想保存和恢复任意数据结构这是一个相当糟糕的想法!
Are you looking for this http://www.boost.org/doc/libs/1_55_0/libs/serialization/doc/index.html[^]?
Haven't tried that myself, so I can't provide help beyond the content of this article. However, a word of warning: it seems the article refers toboost::shared_ptr
, notstd::shared_ptr
! The two may not be the same!
Also the article makes a point to archive the reference counter - this may be useful if your goal is to save the internal state of your program, e. g. for debugging purposes. But I consider it a rather bad idea if you just want to save and restore arbitrary data structures!
这篇关于使用共享指针和模板增强系列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文