On Fri, Mar 23, 2001 at 09:20:22AM -0500, David E. Cleary wrote:
> 
> 
> > -----Original Message-----
> > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of John
> > Snelson
> >
> > The C++ way of doing this is the following code fragment:
> >
> > if(node.getNodeType() == DOM_Node::ELEMENT_NODE) {
> >   DOM_Element element = static_cast<DOM_Element&>(node);
> > }
> 
> Wouldn't dynanic_cast be more what Curt is looking to do, as that will
> actually perform a runtime check to see in node is indeed a DOM_Element?
> I've never used these casting constructs myself, so I am interested in
> hearing other's thoughts on them.

The cleanest way to do casting is using class specific cast operator or cast
constructor.
        element = (DOM_Element) node
is clean (???) cast in Java but it does not work in C++.
        element = static_cast<DOM_Element&>(node);
says "I do know what I do" (what about the future?)


The solution 1 (cast operator):
class DOM_Node
{
        ...
        operator DOM_Element(void);
};

The solution 2 (cast constructor):
class DOM_Element
{
        ...
        DOM_Element (const DOM_Node&);
};

Both solution are better then direct cast because the casts are method calls
which can do what they want to guarantee the correct casting. For invalid
conversion one could throw a appropriate exception. I (and many others as I
guess) would wellcome one of them in Xerces DOM.

regards.
-- 
Miroslaw Dobrzanski-Neumann

MOSAIC SOFTWARE AG
Base Development and Research
Tel +49-2225-882-291
Fax +49-2225-882-201
E-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to