Muhammad Fahad Baig
Muhammad Fahad Baig

Reputation: 15

c++ Serializing an object containing string and pointer to another object

This question is part of my University Lab work!

I have two classes, student and course. A basic interface for both classes is shown below:

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();
};

I've been asked to write the student object (only the member variables) to a file in binary mode. Now I understand I have to serialize the object but I've no idea how should I proceed. I know C++ provides basic serialization for basic types (correct me if I'm wrong) but I've no idea how will I serialize a string and a course records variable in student object (which is a dynamically allocated array) to a file.

Please ask if you need any extra thing. Thanks!

Upvotes: 0

Views: 2435

Answers (2)

senex
senex

Reputation: 447

Because you are only trying to serialize the member variables the problem is rather trivial. Here is a small example that shows how to serialize such a simple sequence of variables into a continuous array of bytes (chars). I didn't test the code but the concept should be clear enough.

// serialize a string of unknown lenght and two integers
// into a continuous buffer of chars
void serialize_object(student &stu, char *buffer)
{
    // get a pointer to the first element in the
    // buffer array
    char *char_pointer = buffer;
    // iterate through the entire string and
    // copy the contents to the buffer
    for(char& c : stu.name)
    {
        *char_pointer = c;
        ++char_pointer;
    }
    // now that all chars have been serialized we
    // cast the char pointer to an int pointer that
    // points to the next free byte in memory after
    // the string
    int *int_pointer = (int *)char_pointer;
    *int_pointer = stu.id;
    // the compiler will automatically handle the appropriate
    // byte padding to use for the new variable (int)
    ++int_pointer;
    // increment the pointer by one size of an integer
    // so its pointing at the end of the string + integer buffer
    *int_pointer = stu.course_count;
}

Now the buffer variable points to the start of a continuous array of memory that contains the string and both integer variables packed into bytes.

Upvotes: 0

Pavan Chandaka
Pavan Chandaka

Reputation: 12781

You have best answers from ISO CPP standard.

I can not explain better than that.

Please walk through question numbers (4,9,10,11) for answers to your specific question.

https://isocpp.org/wiki/faq/serialization

Upvotes: 2

Related Questions