I have four classes ( A , B , C and D ), following the classic diamond pattern and the Container class containing unique_ptr<A> . I want to serialize these classes using the cereal serialization library.
struct A {int f1; int f2; int f3} struct B : public virtual A { template<typename Archive> inline void save(Archive& ar) const { std::cerr << "Saving Obj: " << this << std::endl; std::cerr << "This: " << &(this->f1) << " " << &(this->f2) << " " << &(this->f3) << std::endl; std::cerr << "This: " << this->f1 << " " << this->f2 << " " << this->f3 << std::endl; }; } }; struct C : public virtual A {}; struct D : public B, public C {}; #include <cereal/archives/binary.hpp> CEREAL_REGISTER_TYPE(B); CEREAL_REGISTER_TYPE(C); CEREAL_REGISTER_TYPE(D); struct Container { std::unique_ptr<A> obj; template<typename Archive> inline void save(Archive& ar) const { std::cerr << "Saving Container" << std::endl; std::cerr << "Obj Addr: " << obj.get() << std::endl; std::cerr << "Obj: " << &(obj->f1) << " " << &(obj->f2) << " " << &(pq->f3) << std::endl; std::cerr << "Obj: " << " " << pq->sq_count << " " << pq->sq_bits << " " << pq->dim << std::endl; ar(obj); // Call serialization for obj, ie B.save(...) } }
All classes have grain functions save and load , but I have included them only for B and Container , since they are the only ones used in this example.
I use these classes as follows:
std::unique_ptr<A> obj(new B); obj->f1 = 8; obj->f2 = 8; obj->f3 = 128; std::unique_ptr<Container> db(new Container); db.obj = std::move(obj); std::ofstream out_file(out_filename); cereal::BinaryOutputArchive out_archive(out_file); out_archive(db);
And I get the following output:
Saving Container Obj Addr: 0x23d2128 Obj: 0x23d2130 0x23d2134 0x23d2138 // Fields adresses (f1,f2,f3) Obj: 8 8 128 // Fields values Saving Obj: 0x23d2128 // Same object This: 0x23d2118 0x23d211c 0x23d2120 // Different field adresses ! This: 4293296 0 37569440 // Garbage
My question is: is it possible that this is a mistake in cereals, or is there something that I do not get with virtual inheritance?
Are field addresses of this object expected to ever change in a C ++ program?
source share