Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm new to C++ and how do i serialize the struct having shared pointer and template . Below is sample code.
C++
#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;
    }
}
}

now how do i implement in the corresponding Mydata.cpp file for shared pointer ?
Posted
Updated 5-Mar-14 1:25am
v2

1 solution

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 to boost::shared_ptr, not std::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!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900