I have a base class User that is serializable:
class User { public: User(); std::string GetLogin() const; void SetLogin(std::string login); protected: std::string mLogin; friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & mLogin; } };
This class can be inherited by another class as follows:
class UserA : public User { UserA(); private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & boost::serialization::base_object<User>(*this); ar & mIsSomething; } bool mIsSomething = true; }
To handle this user, I have a class "manager" that contains the user vector:
class Manager { public: bool Add(User user); bool Remove(unsigned int index); private: std::vector<User> mUsers; friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & mUsers; } };
Thus, my manager can be populated with UserA or UserB (both do not work at the same time). When I retrieve an item from Manager , I simply return it back to the correct child class. This part is working fine.
But when I want to serialize the Manager class, obviously, Boost does not know what type of User I am trying to serialize, and the additional fields from the child class are not serialized.
What is my solution here? Is my project completely wrong?
Should I specialize my manager class to something like this?
class Manager { bool Add(UserA user); bool Add(UserB user); private: std::vector<UserA> mUsersA; std::vector<UserB> mUsersB; }
c ++ inheritance boost serialization boost-serialization
grunk Oct 27 '15 at 10:12 2015-10-27 10:12
source share