Re: [RTTI] cast base class pointer to <templated> derived class pointer
From: Ivan Vecerina (ivecATmyrealboxDOTcom)
Date: 10/12/03
- Next message: David B. Held: "Re: new"
- Previous message: Phlip: "Re: Converting pointer-to-member to int"
- In reply to: tirath: "[RTTI] cast base class pointer to <templated> derived class pointer"
- Next in thread: tirath: "Re: [RTTI] cast base class pointer to <templated> derived class pointer"
- Reply: tirath: "Re: [RTTI] cast base class pointer to <templated> derived class pointer"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 12 Oct 2003 09:04:45 +0200
"tirath" <int19h@xsmail.com> wrote in message
news:3f88b7eb@news.comindico.com.au...
...
> I'm building (trying to anyway) a serialization library. Here's my design:
>
> SPair<SP1, SP2> derives from Pair<P1,P2> and Ser:
...
> the list holds Ser* - i.e. base class pointers.
...
> At some point, the user may call a static method Ser::Ser2Disk (at the
> moment just Ser::Ser2StdOut) that serializes all SObjects that have
> registered for serialization by iterating though the serialization list,
> which is a list of base class pointers. The problem is, what to do with
the
> base class pointers that I get off the serialization list. (I had a look
at
> the serialization stuff in the FAQ: the impression I got was that for
> serialisation, the way suggested is to simply call each SObject's
> Serialize() method... which is okay, but could be a lot better, why not
with
> just a single Serialize()
What do you mean by a 'single' Serialize() ?
AFAICT, different code needs to be generated for each template instance
of SPair that is used in your program. (If not, please clarify...).
The easiest way to generated these multiple Serialize() routines is to
make Serialize() a member function of the SPair template.
Then you still need a mechanism to access the Serialize() routine
that matches the object instance you currently have a reference to.
The native C++ way of doing this is to use a virtual method
declared in the base class.
Therefore:
in class Ser:
virtual void Serialize(_some_params_)=0;
in template class SPair:
virtual void Serialize(_some_params_)
{ /* code to serialize each member of the pair */ }
> If I dynamic_cast the with full type information (as in `SPair<int, float>
> *ptr = dynamic_cast <SPair<int, float>*> (*i);`) I get the desired derived
> class, but how can I do this in a fully templated way? I suspect it may
take
> a drastic change in design (since SPair<int, float> is essentially a
> different type than SPair<char *, double>). Any suggestions?
Is there a reason why you do not want to consider a virtual method ?
There are contorted ways to avoid using this simple and efficent
approach (see "Modern C++ Design" to see how sophisticated things
can become). But it is unclear to me why you do not want to use it here.
Regards,
Ivan
-- http://ivan.vecerina.com
- Next message: David B. Held: "Re: new"
- Previous message: Phlip: "Re: Converting pointer-to-member to int"
- In reply to: tirath: "[RTTI] cast base class pointer to <templated> derived class pointer"
- Next in thread: tirath: "Re: [RTTI] cast base class pointer to <templated> derived class pointer"
- Reply: tirath: "Re: [RTTI] cast base class pointer to <templated> derived class pointer"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|