C++中序列化对象需手动实现,常用方法有:1. 二进制I/O直接读写简单对象,适用于无指针的基本类型;2. 自定义读写函数处理含string等复杂成员的对象;3. 使用Boost.Serialization库支持STL容器、继承等复杂结构,推荐用于复杂场景;4. 结合nlohmann/json等库转为JSON格式,适合跨平台与可读性需求。选择依据对象复杂度与应用需求。
在C++中,序列化对象到文件没有像Java或C#那样的内置机制,需要手动实现。常用的方法包括使用
iostream
配合
read()
和
write()
、重载操作符、借助第三方库(如Boost)等。以下是几种常见且实用的C++对象序列化方法。
1. 使用二进制I/O直接写入简单对象
适用于只包含基本数据类型(如int、double、char数组)且不含指针或动态内存的类。
通过
ofstream::write()
和
ifstream::read()
直接将对象内存块写入文件。
示例代码:
注意:这种方法不能用于含有指针、string、vector等复杂成员的类,否则会导致未定义行为。
2. 手动序列化复杂对象(自定义读写)
适用于包含字符串、容器或动态资源的对象。
将对象逐个字段写入文本或二进制文件,反序列化时按相同顺序读取。
立即学习
“
C++免费学习笔记(深入)
”;
示例:序列化含std::string的类
Eclipse导入Android或其他的JAVA项目的正确方法 WORD版
本文档主要讲述的是Eclipse导入Android或其他的JAVA项目的正确方法;希望本文档会给有需要的朋友带来帮助;感兴趣的朋友可以过来看看
下载
使用方式:
3. 使用Boost.Serialization库(推荐)
Boost提供强大且灵活的序列化支持,支持STL容器、继承、指针等复杂结构。
需安装Boost,并包含相应头文件。
示例:
支持格式:text(可读)、binary、xml。编译时需链接Boost库。
4. 使用JSON或其他格式(现代C++常用)
结合nlohmann/json等库,将对象转为JSON字符串存储,适合配置或跨平台场景。
示例:
基本上就这些。选择哪种方式取决于对象复杂度和是否需要可读性、跨平台兼容性。简单结构可用二进制IO,复杂对象推荐Boost或JSON方案。
#include
#include
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(&p), sizeof(Point));
out.close();
// 反序列化
Point p2;
std::ifstream in("point.dat", std::ios::binary);
in.read(reinterpret_cast(&p2), sizeof(Point));
in.close();
std::cout << "x: " << p2.x << ", y: " << p2.y << std::endl;
return 0;
}
#include
#include
class Person {
public:
std::string name;
int age;
void save(std::ofstream& out) const {
size_t len = name.size();
out.write(reinterpret_cast(&len), sizeof(len));
out.write(name.c_str(), len);
out.write(reinterpret_cast(&age), sizeof(age));
}
void load(std::ifstream& in) {
size_t len;
in.read(reinterpret_cast(&len), sizeof(len));
name.resize(len);
in.read(&name[0], len);
in.read(reinterpret_cast(&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();
#include
#include
#include
#include
#include
class Person {
friend class boost::serialization::access;
template
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
#include
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); // 格式化输出
