0

Interface * base_ptr1 = new Derived1();

Interface * base_ptr2 = new Derived2();

In theory I should have serialize() method in all classes but my interface type class does not have data members, only abstract virtual methods.

ionyy
  • 1
  • 4
  • "my interface type class does not have data members" So what's the problem with that? – n. m. could be an AI Jul 11 '21 at 08:45
  • I used boost for serialization and boost::archive so I did not know about using istream and ostream – ionyy Jul 11 '21 at 08:56
  • I tried to implement solution by reading this link https://www.boost.org/doc/libs/1_72_0/libs/serialization/doc/tutorial.html, so I couldn't find anything about interface type – ionyy Jul 11 '21 at 08:58

1 Answers1

0

You could add pure virtual functions to do the serialization.

class Interface {
public:
    virtual void read(std::istream& is) = 0;
    virtual void write(std::ostream& os) const = 0;
};

std::istream& operator>>(std::istream& is, Interface& i) {
    i.read(is);
    return is;
}

std::ostream& operator<<(std::ostream& os, const Interface& i) {
    i.write(os);
    return os;
}

Now, the concrete classes only needs to implement read and write and the streaming operators implemented for Interface will work for them all.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108