|
Boost Users : |
From: Robert Ramey (ramey_at_[hidden])
Date: 2007-02-15 23:15:35
Aljaz wrote:
> Hey
>
> I took a look at smart pointer example..
> However, I need different approach in the following code:
>
> spa = boost::shared_ptr<A>(new B);
> spa1 = spa;
> display(spa, spa1);
> // serialize it
> {
> std::ofstream ofs(filename.c_str());
> boost::archive::text_oarchive oa(ofs);
> oa.register_type(static_cast<B *>(NULL));
> oa << spa;
> oa << spa1;
> }
>
> Do I have to register_type this way? Can I use BOOST_EXPORT ..
> instead?
Yes
>When I want to deserialize data, the derived class will be
> unknown, so I have to deserialize data with base class shared
> pointer..
> This will then contain information - type of derived class, and then
> I will cast it to derived class..
You may not have to do this. And you may not want to. The typical
use case for using an abstract base class is to have all the functions
in the derived class declared private and be called only through the
the virtual function table in the abstract base class. The separates
the interface defined in the abstract base class defined from the specific
implementation in the derived class.
> How could I do this?
a) You can embed a function in the base class like who_am_i() which
returns an indictator of what kind of class it "really" is.
b) you can use a function from the extended_type_info family of
the serialization library to do something similar
Neither of these are recommend as far as I'm concerned. The best
is to make all the functions you want to call virtual and call them through
the base class. This topic is addressed in Scott Meyers "Effective C++"
Another effect is that you can add more derived classes without even
recompiling the code you're already using. There is a demo in the
serialization library which illustrates this.
>
> Thank you very much
>
you're welcome
Robert Ramey
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net