久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

你如何在 C++ 中序列化一個對象?

How do you serialize an object in C++?(你如何在 C++ 中序列化一個對象?)
本文介紹了你如何在 C++ 中序列化一個對象?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個小的對象層次結構,我需要通過套接字連接序列化和傳輸這些對象.我需要序列化對象,然后根據它的類型反序列化它.在 C++ 中有沒有一種簡單的方法可以做到這一點(就像在 Java 中一樣)?

是否有 C++ 序列化在線代碼示例或教程?

明確地說,我正在尋找將對象轉換為字節數組,然后再轉換回對象的方法.我可以處理套接字傳輸.

解決方案

談序列化,boost 序列化 API.至于通過網絡傳輸序列化數據,我要么使用伯克利套接字,要么使用 asio 庫.>


如果要將對象序列化為字節數組,可以按以下方式使用 boost 序列化器(取自教程站點):

#include #include 類 gps_position{私人的:友元類 boost::serialization::access;模板<類存檔>void serialize(Archive & ar, const unsigned int version){和度;和分鐘;和秒;}整數度;整數分鐘;浮動秒;民眾:gps_position(){};gps_position(int d, int m, float s) :度(d)、分(m)、秒(s){}};

實際的序列化非常簡單:

#include std::ofstream ofs("filename.dat", std::ios::binary);//創建類實例const gps_position g(35, 59, 24.567f);//保存數據到存檔{boost::archive::binary_oarchive oa(ofs);//將類實例寫入存檔oa<<G;//調用析構函數時存檔和流關閉}

反序列化的工作方式類似.

還有一些機制可以讓您處理指針的序列化(復雜的數據結構,如 tres 等沒有問題),派生類,您可以在二進制和文本序列化之間進行選擇.此外,所有 STL 容器都是開箱即用的.

I have a small hierarchy of objects that I need to serialize and transmit via a socket connection. I need to both serialize the object, then deserialize it based on what type it is. Is there an easy way to do this in C++ (as there is in Java)?

Are there any C++ serialization online code samples or tutorials?

EDIT: Just to be clear, I'm looking for methods on converting an object into an array of bytes, then back into an object. I can handle the socket transmission.

解決方案

Talking about serialization, the boost serialization API comes to my mind. As for transmitting the serialized data over the net, I'd either use Berkeley sockets or the asio library.

Edit:
If you want to serialize your objects to a byte array, you can use the boost serializer in the following way (taken from the tutorial site):

#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
class gps_position
{
private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & degrees;
        ar & minutes;
        ar & seconds;
    }
    int degrees;
    int minutes;
    float seconds;

public:
    gps_position(){};
    gps_position(int d, int m, float s) :
    degrees(d), minutes(m), seconds(s)
    {}
};

Actual serialization is then pretty easy:

#include <fstream>
std::ofstream ofs("filename.dat", std::ios::binary);

    // create class instance
    const gps_position g(35, 59, 24.567f);

    // save data to archive
    {
        boost::archive::binary_oarchive oa(ofs);
        // write class instance to archive
        oa << g;
        // archive and stream closed when destructors are called
    }

Deserialization works in an analogous manner.

There are also mechanisms which let you handle serialization of pointers (complex data structures like tress etc are no problem), derived classes and you can choose between binary and text serialization. Besides all STL containers are supported out of the box.

這篇關于你如何在 C++ 中序列化一個對象?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉換為 HSV 并將 HSV 轉換為 RGB 的算法,范圍為 0-255)
How to convert an enum type variable to a string?(如何將枚舉類型變量轉換為字符串?)
When to use inline function and when not to use it?(什么時候使用內聯函數,什么時候不使用?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意義;cin.tie(NULL);)
Is TCHAR still relevant?(TCHAR 仍然相關嗎?)
主站蜘蛛池模板: 亚洲精品在线观看视频 | 国产99在线 | 日韩一区二区在线观看视频 | 伊人网av| 精品免费国产 | 国产欧美一区二区三区在线看蜜臀 | 日本黄页视频 | 天天干夜夜骑 | 欧美国产日韩精品 | 极品白嫩少妇无套内谢 | 国产乱国产乱300精品 | 中文字幕日韩欧美 | 超碰免费在线观看 | 在线网站免费观看18 | 99色在线 | 天天干天天操天天摸 | 欧美人与性动交α欧美精品 | 人人草人人爽 | 精品久久久久久久久久久 | 国产三级在线看 | 中文字幕免费看 | 国产高清一区二区 | 国产欧美精品一区二区色综合 | 国产精品久久久久久久久久久久久久 | 日韩1级片 | 精品一区二区在线播放 | 秋霞午夜鲁丝一区二区老狼 | 欧美精品乱码视频一二专区 | 国产成人免费观看 | 国产日本在线观看 | 他揉捏她两乳不停呻吟动态图 | 一个色综合网 | 成人毛片网站 | 国产一区二区三区在线看 | 亚洲国产第一页 | 99一区二区| 精品欧美日韩 | 天堂影院av| 精品黄色| 欧美日韩高清 | 日韩欧美视频在线 |