Re: [Boost-users] Derived class is not serialising/de-serialising...
Hello,
My previous post contained a minimal example, which was incomplete,
deliberately so as not to bloat the post with code. However Jeff Flynn
has requested a minimal code example.
I have attached several files for classes to this e-mail.
* BaseClass - the base class, which has a serialize method
* DerivedClass - a sub-class with its own serialize method
* InstantiableClass - the class actually instantiated from DerivedClass
* Main - code example of the client/server that serializes and
de-serializes the InstantiableClass object.
Our system automatically inserts some pre-compiled headers so
to get the code to compile you may need to add your own #includes.
The output from the main program is this:
Serializing...
BOOST: I am an Instantiable class object
BOOST: I am in BaseClass::serialize
De-serializing...
BOOST: I am an Instantiable class object
BOOST: I am in BaseClass::serialize
Assertion failed: dynamic_cast
Marcus MacWilliam wrote:
Hello,
My previous post contained a minimal example, which was incomplete, deliberately so as not to bloat the post with code. However Jeff Flynn has requested a minimal code example.
I have attached several files for classes to this e-mail.
BaseClass - the base class, which has a serialize method DerivedClass - a sub-class with its own serialize method InstantiableClass - the class actually instantiated from DerivedClass Main - code example of the client/server that serializes and de-serializes the InstantiableClass object. Our system automatically inserts some pre-compiled headers so to get the code to compile you may need to add your own #includes.
The output from the main program is this:
Serializing... BOOST: I am an Instantiable class object BOOST: I am in BaseClass::serialize De-serializing... BOOST: I am an Instantiable class object BOOST: I am in BaseClass::serialize Assertion failed: dynamic_cast
(object).getCom() == 20, file Main.cc, line 8 This highlights the polymorphism problem in a nutshell. The overloaded what() method is telling me that in both the client and server code the objects is an InstantiableClass object. However the Derived::serialize is not being called on either side. Thus the data (m_com) from the DerivedClass is not being serialized and is lost.
The compiler, and debugger, is telling me my object is an InstantiableClass, however when boost calls serialize on the object, the code in the BaseClass and not the Derived class is being called.
Any help greatly appreciated.
At first I thought this was obvious. But now I look at this I would have thought it would work. I'm thinkinig it's related to the member function look up rules associated with virtual member functions. In any case, try the following: a) in BaseClass.h what(); to what() = 0; This will detect any non-obvious copying (slicing) which might be occurring. b) in InstantiableClass add a serialize function which only incudes only base object serialization. c) tweak your main test program so that it doesn't use InstantiableClass but rather DerivedClass directly. Does that work? Robert Ramey
At first I thought this was obvious. But now I look at this I would have thought it would work. I'm thinkinig it's related to the member function look up rules associated with virtual member functions. In any case, try the following:
a) in BaseClass.h what(); to what() = 0; This will detect any non-obvious copying (slicing) which might be occurring. The what was added just for debugging purposes. Removal of all what() still does not cause DeriveClass::serialize to be called.
b) in InstantiableClass add a serialize function which only incudes only base object serialization. If I did that, I would lose the data from the derived class. (m_com).
c) tweak your main test program so that it doesn't use InstantiableClass but rather DerivedClass directly. Does that work? It needs to use the InstantiableClass, cannot use the DerivedClass directly. This is cut down code from our application, with a single example of the relationship. The classes are not called by those names IRL.
Instead there are several classes that are sub-classes of BaseClass, and several classes that are sub-classes of DerivedClass.
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Marcus MacWilliam wrote:
At first I thought this was obvious. But now I look at this I would have thought it would work. I'm thinkinig it's related to the member function look up rules associated with virtual member functions. In any case, try the following:
a) in BaseClass.h what(); to what() = 0; This will detect any non-obvious copying (slicing) which might be occurring.
The what was added just for debugging purposes. Removal of all what() still does not cause DeriveClass::serialize to be called.
the "=0" makes the class an abstract base class which cannot be instantiated. This will fail to compile code which unintentionally slices the object. Just put the =0 and see what it does.
b) in InstantiableClass add a serialize function which only incudes only base object serialization.
If I did that, I would lose the data from the derived class. (m_com).
I meant base object as it's related to InstanciableClass - in this case that means that the base object referred to by Instancialble class would be the "DerivedClass"
c) tweak your main test program so that it doesn't use InstantiableClass but rather DerivedClass directly. Does that work?
It needs to use the InstantiableClass, cannot use the DerivedClass directly. This is cut down code from our application, with a single example of the relationship. The classes are not called by those names IRL.
My question was "Does that work?" Robert Ramey
participants (2)
-
Marcus MacWilliam
-
Robert Ramey