Compiler does not give error for missing serialize function
Hi I have the following code : class MyClass { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar, const unsigned int version) {} }; class MyClass2 : public MyClass {}; MyClass2* ss = new MyClass2; std::ofstream ofs("C:\\test.xml"); boost::archive::xml_oarchive oa(ofs); oa & boost::serialization::make_nvp("test",ss); I am serializing trough the derived class pointer. The derived class doesnot have serialize function. The Base have. And I was expecting compiler to give me error that MyClass2 doesnot have serialize function. But this code compiles without errors. I am using vs2010. -- View this message in context: http://boost.2283326.n4.nabble.com/Compiler-does-not-give-error-for-missing-... Sent from the Boost - Users mailing list archive at Nabble.com.
On 4/22/16 5:38 AM, Elizabeta wrote:
Hi I have the following code :
class MyClass { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar, const unsigned int version) {} };
class MyClass2 : public MyClass {};
MyClass2* ss = new MyClass2; std::ofstream ofs("C:\\test.xml"); boost::archive::xml_oarchive oa(ofs); oa & boost::serialization::make_nvp("test",ss);
I am serializing trough the derived class pointer. The derived class doesnot have serialize function. The Base have. And I was expecting compiler to give me error that MyClass2 doesnot have serialize function. But this code compiles without errors. I am using vs2010.
the friend gives the serialization library access to your private serialization function. But I was surprised by this as well. I don't think it's wrong though. Robert Ramey
Hi but private functions from base are not inherited in the derived right? so serialize should not be seen in the derived class imho. Also why is not wrong, one can forget to write serialize for the derived class. -- View this message in context: http://boost.2283326.n4.nabble.com/Serialization-Compiler-does-not-give-erro... Sent from the Boost - Users mailing list archive at Nabble.com.
On 4/22/16 10:14 AM, Elizabeta wrote:
Hi but private functions from base are not inherited in the derived right? so serialize should not be seen in the derived class imho. Also why is not wrong, one can forget to write serialize for the derived class.
LOL - good point - you're clearly a smart person. But there's a catch here. people want to be able to serialize private members. So the serialization library calls through the serialization::access which is a friend. This "works around" the normal way of calling a function from a derived class so the "private" is in effect broken. So serialization::access basically converts a private "serialize" function so it means "private except for serialization". You can see this be puting a debug trap on the serialize function and running under the debugger and then checking the stack. That should make clear. Robert Ramey
-- View this message in context: http://boost.2283326.n4.nabble.com/Serialization-Compiler-does-not-give-erro... Sent from the Boost - Users mailing list archive at Nabble.com.
On 4/22/16 1:14 PM, Elizabeta wrote:
Hi but private functions from base are not inherited in the derived right? so serialize should not be seen in the derived class imho. Also why is not wrong, one can forget to write serialize for the derived class.
Derived classes can SEE private base members/functions, they can not access them. This can have effect with overloading (which uses visibility, not accessibility into account) -- Richard Damon
participants (3)
-
Elizabeta
-
Richard Damon
-
Robert Ramey