C++中序列化对象需手动实现,常用方法有:1. 二进制I/O直接读写简单对象,适用于无指针的基本类型;2. 自定义读写函数处理含string等复杂成员的对象;3. 使用Boost.Serialization库支持STL容器、继承等复杂结构,推荐用于复杂场景;4. 结合nlohmann/json等库转为JSON格式,适合跨平台与可读性需求。选择依据对象复杂度与应用需求。

在C++中,序列化对象到文件没有像Java或C#那样的内置机制,需要手动实现。常用的方法包括使用 iostream 配合 read() 和 write()、重载操作符、借助第三方库(如Boost)等。以下是几种常见且实用的C++对象序列化方法。
通过 ofstream::write() 和 ifstream::read() 直接将对象内存块写入文件。
示例代码:
#include <fstream>
#include <iostream>
class Point {
public:
    int x, y;
    Point(int x = 0, int y = 0) : x(x), y(y) {}
};
int main() {
    Point p(3, 7);
    // 序列化
    std::ofstream out("point.dat", std::ios::binary);
    out.write(reinterpret_cast<const char*>(&p), sizeof(Point));
    out.close();
    // 反序列化
    Point p2;
    std::ifstream in("point.dat", std::ios::binary);
    in.read(reinterpret_cast<char*>(&p2), sizeof(Point));
    in.close();
    std::cout << "x: " << p2.x << ", y: " << p2.y << std::endl;
    return 0;
}
将对象逐个字段写入文本或二进制文件,反序列化时按相同顺序读取。
立即学习“C++免费学习笔记(深入)”;
示例:序列化含std::string的类
#include <fstream>
#include <string>
class Person {
public:
    std::string name;
    int age;
    void save(std::ofstream& out) const {
        size_t len = name.size();
        out.write(reinterpret_cast<const char*>(&len), sizeof(len));
        out.write(name.c_str(), len);
        out.write(reinterpret_cast<const char*>(&age), sizeof(age));
    }
    void load(std::ifstream& in) {
        size_t len;
        in.read(reinterpret_cast<char*>(&len), sizeof(len));
        name.resize(len);
        in.read(&name[0], len);
        in.read(reinterpret_cast<char*>(&age), sizeof(age));
    }
};
使用方式:
Person p{"Alice", 25};
// 写入
std::ofstream out("person.dat", std::ios::binary);
p.save(out);
out.close();
// 读取
Person p2;
std::ifstream in("person.dat", std::ios::binary);
p2.load(in);
in.close();
需安装Boost,并包含相应头文件。
示例:
#include <boost/serialization/string.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <fstream>
class Person {
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version) {
        ar & name;
        ar & age;
    }
public:
    std::string name;
    int age;
    Person() {} // 默认构造函数必需
    Person(std::string n, int a) : name(n), age(a) {}
};
// 保存
void save() {
    Person p("Bob", 30);
    std::ofstream os("person.txt");
    boost::archive::text_oarchive oa(os);
    oa << p;
}
// 加载
void load() {
    Person p;
    std::ifstream is("person.txt");
    boost::archive::text_iarchive ia(is);
    ia >> p;
    std::cout << p.name << ", " << p.age << std::endl;
}
示例:
#include <nlohmann/json.hpp>
#include <fstream>
using json = nlohmann::json;
class Person {
public:
    std::string name;
    int age;
    // 转为JSON
    json to_json() const {
        return json{{"name", name}, {"age", age}};
    }
    // 从JSON恢复
    static Person from_json(const json& j) {
        Person p;
        p.name = j.at("name");
        p.age = j.at("age");
        return p;
    }
};
// 使用
Person p{"Charlie", 35};
json j = p.to_json();
std::ofstream o("person.json");
o << j.dump(4); // 格式化输出
以上就是c++++中如何序列化对象到文件_c++对象序列化方法的详细内容,更多请关注php中文网其它相关文章!
 
                        
                        c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
 
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号