【发布时间】:2017-03-28 17:24:16
【问题描述】:
这个问题是我大学实验室工作的一部分!
我有两个班级,student 和 course。两个类的基本接口如下所示:
student.h
class student
{
private:
int id;
string name;
int course_count;
public:
student();
student * student_delete(int);
void student_add(int);
static student * student_add_slot(int);
int get_id();
int get_course_count();
string get_name();
void set_course_count(int);
void student_display();
course * course_records;
void operator= (const student &);
};
course.h
class course
{
private:
string id;
short credit_hours;
string description;
public:
course();
course * remove_course(course*, int);
static course * add_course_slots(course*, int);
int add_course(course*,int);
string get_id();
string get_description();
void course_display(int);
short get_credit_hours();
};
我被要求以二进制模式将 student 对象(仅成员变量)写入文件。现在我明白我必须序列化对象,但我不知道应该如何进行。我知道 C++ 为基本类型提供了基本序列化(如果我错了,请纠正我)但我不知道如何在 student 中序列化一个字符串和一个 course records 变量 > 对象(这是一个动态分配的数组)到一个文件。
请询问您是否需要任何额外的东西。谢谢!
【问题讨论】:
-
您想仅序列化成员变量还是整个类?
-
@AlexHG 只有类的成员变量(我假设你的意思也是函数序列化)。我将对其进行编辑以使其清楚。
-
您还应该查看 boost 序列化库。
-
@jwimberley 谢谢!我会看看。由于这是一项实验室工作,我必须遵守要使用的要求和方法。
标签: c++ serialization